1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
[[email protected] test]
class Bird(object): feather = True
class Chicken(Bird): fly = False def __init__(self,age): self.age = age
chick = Chicken(2)
print(Bird.__dict__) print(Chicken.__dict__) print(chick.__dict__) print('-------------------') chick.__dict__['age'] = 3 print(chick.__dict__['age']) chick.age = 10 print(chick.age)
[[email protected] test] {'__dict__': <attribute '__dict__' of 'Bird' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Bird' objects>, 'feather': True, '__doc__': None} {'fly': False, '__module__': '__main__', '__doc__': None, '__init__': <function __init__ at 0x7f9bce427758>} {'age': 2} ------------------- 3 10
[[email protected] test]
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 also deleted') del self.value neg = property(getNeg,setNeg,delNeg,"I'm negative")
x = num(1.1) print(x.neg)
x.neg = -22 print(x.value) print(num.neg.__doc__) del x.neg
[[email protected] test] -1.1 22 I'm negative value also deleted
[[email protected] test]# cat c.py #!/usr/bin/env python 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) summer.age = 0.5 print(summer.adult) print(summer.male)
[[email protected] test]# python c.py True False Traceback (most recent call last): File "c.py", line 29, in <module> print(summer.male) File "c.py", line 19, in __getattr__ raise AttributeError(name) AttributeError: male
|
近期评论