作者goshan (yah)
看板Python
標題[問題] 關於TypeError: missing.....'self'
時間Fri Dec 6 17:02:55 2019
下面這段程式中, 我預期會得到目前的日期加上字串In prtn_global_config.
可是執行後出現TypeError: prtn_global_config() missing 1 required positional argument: 'self'
error highlight處在最後一行prtn_global_config()
請教問題是出在哪裡呢? 謝謝.
PS. 我使用Sublime, python 3.6.8
import logging
import sys
import random
import time
import datetime
def prtn_info(self, msg):
print(str(datetime.datetime.now()) + msg)
#datetime.datetime.now():獲取當前datetime
def prtn_global_config(self):
self.prtn_info('In prtn_global_config')
prtn_global_config()
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.46.213 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1575622977.A.49A.html
推 TitanEric: 沒有物件怎麼有self 12/06 18:13
推 hongyan: 包在class的函數才需要加self 12/06 18:28
推 garyl62327: Class 的 member function 才需要加上 self,這個self 12/09 08:07
→ garyl62327: 相當於是物件本身,用c++的角度來看就是*this的意思 12/09 08:07