[問題] 關於TypeError: missing.....'self'

看板Python作者 (yah)時間4年前 (2019/12/06 17:02), 編輯推噓3(301)
留言4則, 3人參與, 4年前最新討論串1/1
下面這段程式中, 我預期會得到目前的日期加上字串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

12/06 18:13, 4年前 , 1F
沒有物件怎麼有self
12/06 18:13, 1F

12/06 18:28, 4年前 , 2F
包在class的函數才需要加self
12/06 18:28, 2F

12/09 08:07, 4年前 , 3F
Class 的 member function 才需要加上 self,這個self
12/09 08:07, 3F

12/09 08:07, 4年前 , 4F
相當於是物件本身,用c++的角度來看就是*this的意思
12/09 08:07, 4F
文章代碼(AID): #1TwXb1IQ (Python)