在__init__中直接初始化类属性与声明 [英] direct initialization of class attributes vs. declarations w/in __init__
问题描述
直接在
类定义中初始化类变量与在类'
__init__方法中初始化类变量之间的区别是什么?是否有理由,或许在某些情况下,
选择其中一个?
谢谢。
What''s the difference between initializing class variables within the
class definition directly versus initializing them within the class''s
__init__ method? Is there a reason, perhaps in certain situations, to
choose one over the other?
Thank you.
推荐答案
di ******* ******:gmail.com schrieb:
直接在类定义中初始化类变量与在类中初始化它们之间的区别是什么?是__init__方法?是否有某种原因,或许在某些情况下,选择一个而不是另一个?
What''s the difference between initializing class variables within the
class definition directly versus initializing them within the class''s
__init__ method? Is there a reason, perhaps in certain situations, to
choose one over the other?
您将类变量与实例变量混淆。前者
是你可以在class-statement中初始化的东西。但是,他们
在_all_实例之间共享。考虑这个小例子:
类Foo(对象):
FOO = 1
BAR = []
def __init __(自我,FOO):
self.FOO = FOO
self.BAR.append(FOO)
def __repr __(自我):
返回" FOO:%r \ nBAR:%r \ n" %(self.FOO,self.BAR)
f1 = Foo(1)
打印f1
f2 = Foo(2)
打印f2
打印f1
------
meskal:〜/ Projects / CameraCalibrator deets
You are confusing class variables with instance variables. The former
are what you can initialize inside the class-statement. However, they
are shared amongst _all_ instances. Consider this little example:
class Foo(object):
FOO = 1
BAR = []
def __init__(self, FOO):
self.FOO = FOO
self.BAR.append(FOO)
def __repr__(self):
return "FOO: %r\nBAR: %r\n" % (self.FOO, self.BAR)
f1 = Foo(1)
print f1
f2 = Foo(2)
print f2
print f1
------
meskal:~/Projects/CameraCalibrator deets
python2.4 /tmp/test.py
FOO:1
BAR:[1]
FOO:2
BAR:[1,2]
FOO:1
BAR:[1, 2]
-----
如您所见,列表BAR是共享的。你也可以看到
_assigning_这样的东西:
self.FOO
将创造一个实例变量。即使之前在类上存在同名变量
!
这正是使用变量初始化之间的区别
在__init__中和课堂声明中。
BTW,
self.__class__.FOO = value
>
将在方法中设置类变量。如果你想知道的话。
Diez
python2.4 /tmp/test.py
FOO: 1
BAR: [1]
FOO: 2
BAR: [1, 2]
FOO: 1
BAR: [1, 2]
-----
As you can see, the list BAR is shared. And you can also see that
_assigning_ to something like this:
self.FOO
will create an instance-variable. Even if a variable of the same name
existed on the class before!
Which is precisely the difference between using variable initialization
in __init__ and inside the class-statement.
BTW,
self.__class__.FOO = value
will set class-variables inside a method. Just if you wondered.
Diez
啊,你给我带来了很多清晰度Diez,谢谢。这将解释
一些错误我一直在......
Diez B. Roggisch写道:
Ah, you''ve brought me much clarity Diez, thank you. That would explain
some "bugs" I''ve been having...
Diez B. Roggisch wrote:
di ************* @ gmail.com schrieb:
初始化类变量之间的区别是什么直接在类定义中,而不是在类的
__init__方法中初始化它们?是否有某种原因,或许在某些情况下,选择一个而不是另一个?
What''s the difference between initializing class variables within the
class definition directly versus initializing them within the class''s
__init__ method? Is there a reason, perhaps in certain situations, to
choose one over the other?
您将类变量与实例变量混淆。前者是你可以在class语句中初始化的东西。但是,它们在_all_实例之间共享。考虑这个小例子:
类Foo(对象):
FOO = 1
BAR = []
def __init __(self,FOO):
self.FOO = FOO
self.BAR.append(FOO)
def __repr __(self):
return" FOO:%r \ NBA: %r\\\
" %(self.FOO,self.BAR)
f1 = Foo(1)
打印f1
f2 = Foo(2)
打印f2
打印f1
------
meskal:〜/ Projects / CameraCalibrator deets
You are confusing class variables with instance variables. The former
are what you can initialize inside the class-statement. However, they
are shared amongst _all_ instances. Consider this little example:
class Foo(object):
FOO = 1
BAR = []
def __init__(self, FOO):
self.FOO = FOO
self.BAR.append(FOO)
def __repr__(self):
return "FOO: %r\nBAR: %r\n" % (self.FOO, self.BAR)
f1 = Foo(1)
print f1
f2 = Foo(2)
print f2
print f1
------
meskal:~/Projects/CameraCalibrator deets
这篇关于在__init__中直接初始化类属性与声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!