【发布时间】:2012-12-11 20:17:05
【问题描述】:
可能重复:
Variables inside and outside of a class __init__() function
我知道当一个类被调用时,它会先运行__init__ 中的代码。我还是看不出那和直接在类下写代码的区别。
例如:
class main():
x = 1
def disp(self):
print self.x
class main():
def __init__(self):
self.x = 1
def disp(self):
print self.x
对我来说,两者都具有相同的功能。 (也许我遗漏了一些东西。)我想知道哪个更(咳咳)pythonic 以及为什么。
【问题讨论】:
-
您在输入此问题时没有考虑查看它提出的类似问题吗?
-
哪个?我确实尝试过先搜索。
-
重复项太多。很抱歉!