【发布时间】:2014-01-23 19:15:52
【问题描述】:
我正在学习 Python,但没有 OOP 经验。我在 IDLE (Python 3.3) 中输入以下行,对象的行为让我感到困惑:
>>> class IBAN:
ISOcode = ' '
checkDigits = '00'
class CCC:
bankCode = '0000'
branchCode = '0000'
checkBank = '0'
checkAccount = '0'
account = '0000000000'
>>> >>> numberOne = IBAN()
>>> numberOne.CCC
<class '__main__.IBAN.CCC'>
>>> numberOne.CCC.bankCode
'0000'
>>> numberOne.ISOcode = 'ES'
>>> IBAN.ISOcode
' '
>>> numberOne.ISOcode
'ES'
>>> IBAN.ISOcode = 'FR'
>>> numberOne.ISOcode
'ES'
>>> IBAN.ISOcode = 'FR'
>>> IBAN.ISOcode
'FR'
>>> numberTwo = IBAN()
>>> numberTwo.ISOcode
'FR'
>>> IBAN.ISOcode = 'IT'
>>> IBAN.ISOcode
'IT'
>>> numberOne.ISOcode
'ES'
>>> numberTwo.ISOcode
'IT'
>>> IBAN.ISOcode is numberTwo.ISOcode
True
>>>
为什么对象numberTwo的ISOcode属性与IBAN的ISOcode相同,而与objectnumberOne的ISOcode不同?
我希望是这样的:
>>> numberOne.ISOcode
'ES'
>>> numberTwo.ISOcode
'FR'
但是,这与我的结果不符。
我找到了a similar question on StackOverflow,这让我觉得结果应该是:
>>> numberOne.ISOcode
'IT'
>>> numberTwo.ISOcode
'IT'
但这也不符合我的结果。
任何人都可以解释或链接到解释 Python 中类的行为的资源吗?我尝试学习 Python 教程和 Guido van Rossum 的“Unifying types and classes in Python 2.2”,但我无法理解他们在说什么。
谢谢!
【问题讨论】:
标签: python class oop attributes instances