2006年09月29日 星期五 10:33
如果数据成员不是在 __init__(self) 中初始化的,那么与后面这种方式有什么区别?
class ZDAParser(Parser):
formatter = 'ZDA'
channels = {}
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20060929/b15b4906/attachment.html
2006年09月29日 星期五 10:43
Hi Black Sands,
在 __init__(self) 中定义的是当前这个 instance 的值,而你列出的这个
(formatter, channels),是属于 ZDAParser 这个 class 的。其实类似 C++ 里面
的 data member 和 static data member ...
例子:
class FooBar:
foo = "foo"
def __init__(self):
self.val = 1
print "ctor:", self.foo
print FooBar().val
print FooBar.foo
print FooBar.val # here will crash
Black Sands ( Li Ying Min ) 写道:
> 如果数据成员不是在 __init__(self) 中初始化的,那么与后面这种方式有什么区别?
>
>
> class ZDAParser(Parser):
> formatter = 'ZDA'
> channels = {}
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
Best regards!
Kasicass/sYcini - Coder
http://www.sycini.com
2006年09月29日 星期五 10:47
~æjÛ^ÈZw¥²m§ÿðÃ(¡Ú^rG«¢¸Ý<×÷hsöÊר~Ü÷!w¬{÷!×[4êfm4ëOvôZrDۧȧÛM:Óݽ×M½çªr¶'r§zÇ¥Ël§+a¢w'§+a¢w!w¬z)â¶Ç¥}ÉZ²ÆC ö«±êÏj»ú+«mz¶CÈZw¥°tÌ-DKÛiÿúr¶'r髨¥þÜ)Þ±ïÚ¶Ög§¶ÏöÓN´÷oÓ]ßn¹ý«miÈfz{a¶ip,¹¸ÞrÛj׬۾ԮXm¶ÿ§+a¢w'þ©z¹_éÊØhÈbëý«miÈfz{lÿm4ëOv÷ý7õÝöëÚ¶Ög§¶÷2006年09月29日 星期五 11:06
(.ÊÈ^è¬iìZ^rV¬°ñHãgjg
2006年09月29日 星期五 11:32
python是当赋值是来创建变量绑定的。在你的例子中name在开始时在实例中是不存在的,它只存在于类中。因此当你通过实例来访问name时,它首先在实例属性中查找,当找不到时,它会自动到类的属性中查找。因此你开始看到的是类的属性,通过实例给查出来了。但在后面你给 cxj.name赋值 ,这样就创建了一个新的实例属性,而类中的属性就被屏蔽掉了。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
Zeuux © 2025
京ICP备05028076号