所以我刚刚开始在
python编程,我不明白“自我”背后的整个推理.我知道它几乎像一个全局变量一样使用,这样可以在类中的不同方法之间传递数据.我不明白为什么在同一个类中调用另一个方法时需要使用它.如果我已经在上课了,为什么要告诉它?
例如,如果我有:
为什么我需要self.thing()?
class bla: def hello(self): self.thing() def thing(self): print "hello"
解决方法
你也可以在类静态中使方法不需要自己.但是,如果您真的需要,请使用此功能.
你:
class bla: def hello(self): self.thing() def thing(self): print "hello"
静态版
class bla: @staticmethod def hello(): bla.thing() @staticmethod def thing(): print "hello"