... self.weight = weight
...
>>> class Stilton(Cheese):
... smell = 'bad'
... def __getattr__(self, name):
... if name.startswith('get_'):
... def func():
... return getattr(self, name[4:])
... return func
... else:
... if hasattr(self, name):
... return getattr(self, name)
... else:
... raise AttributeError(name)
...
>>> stilton = Stilton('100g')
>>> print stilton.weight
100g
>>> print stilton.get_weight
>>> print stilton.get_weight()
100g
>>> print stilton.age
Traceback (most recent call last):
File "", line 1, in
File "", line 12, in __getattr__
AttributeError: age
希望本文所述对大家的Python程序设计有所帮助。










