作者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://webptt.com/cn.aspx?n=bbs/Python/M.1575622977.A.49A.html
1F:推 TitanEric: 没有物件怎麽有self 12/06 18:13
2F:推 hongyan: 包在class的函数才需要加self 12/06 18:28
3F:推 garyl62327: Class 的 member function 才需要加上 self,这个self 12/09 08:07
4F:→ garyl62327: 相当於是物件本身,用c++的角度来看就是*this的意思 12/09 08:07