实验记录-python1

目标:

理解通过内置函数property() 及 __getattr__创建属性的过程。

实验过程

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class bird(object):
...     feather = True
... 
>>> class chicken(bird):
...     def __init__(self,age):
...         self.age = age
...     def getAdult(self):
...         if self.age>10: return True
...         else: return False
...     fly = False
...     adult = property(getAdult)
... 
>>> summer = chicken(2)
>>> 
>>> print summer.__dict__
{'age': 2}
>>> print summer.adult
False
>>> print summer.age
2
>>> print summer.fly
False
>>> print summer.feather
True
>>> 
>>> print summer.__class__  # 追溯对象的类
<class '__main__.chicken'>
>>> print chicken.__base__  # 追溯类的父类
<class '__main__.bird'>
>>> print bird.__base__     # 追溯父类的父类
<type 'object'>
>>> print object.__base__
None
>>> 
>>> class num(object):
...     def __init__(self, value):
...         self.value = value
...     def getNeg(self):
...         return -self.value
...     def setNeg(self, value):
...         self.value = -value
...     def delNeg(self):
...         print "value deleted"
...         del self.value
...     neg = property(getNeg, setNeg, delNeg, "I am negative")
... 
>>> x = num(1.1)
>>> print x.neg
-1.1
>>> x.neg = -22
>>> print x.value
22
>>> print num.neg.__doc__
I am negative
>>> del x.neg
value deleted
>>> print x.value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'num' object has no attribute 'value'
>>> x.neg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in getNeg
AttributeError: 'num' object has no attribute 'value'
>>> print x.neg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in getNeg
AttributeError: 'num' object has no attribute 'value'
>>> 

通过__getattr__(self, name)可用来查询即时生成的属性。当通过__dict__方法无法找到该属性,那么Python会调用对象的__getattr__方法,来即时生成该属性。

>>> class bird(object):
...     feather = True
... 
>>> class chicken(bird):
...     fly = False
...     def __init__(self, age):
...         self.age = age
...     def __getattr__(self, name):
...         if name == 'adult':
...             if self.age >1.0: return True
...             else: return False
...         else: raise AttributeError(name)
... 
>>> summer = chicken(2)
>>> 
>>> print summer.adult
True
>>> summer.age = 0.5
>>> 
>>> print summer.adult
False
>>> print summer.male
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in __getattr__
AttributeError: male
>>> 

结论

在class中通过 property函数定义类属性(设置getter, setter和del方法),之后,在对象中即可动态操作这些属性。这些属性(property)不同于类或对象的特性(attribute),后者静态保存于对象的__dict__中。

通过__getattr__可以将所有的即时生成属性放在同一个函数中处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pierre_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值