实例化一个对象,就会自动创建一个该对象的命名空间,在该命名空间中存放对象的属性;同时,在实例化之后,就后产生一个指向类对象指针,用来指向当前对象所属类的命名空间,这样就可以访问类的静态属性与动态属性。
class Family:
'''
定义一个公共账号 ,只要有人上班,就将钱存到这个账号上
'''
share_money = 0 # 不可变数据类型做静态属性
native_place = ['china'] # 可变数据类型做静态属性
def __init__(self, role, name, salary):
self.role = role
self.name = name
self.salary = salary
def work(self):
Family.share_money += self.salary # 将每个的钱都存放到这个公共账号上
print('the account remains ¥%s '%Family.share_money)
member1 = Family('father', 'lilei', 1000)
member2 = Family('mother', 'zhanghua', 500)
member1.work() # the account remains ¥1000
member2.work() # the account remains ¥1500
member1.share_money = 200 # 为自己独立开了个小金库,并存入200元 -- 在对象member1中添加这一属性
member1.share_money += 100 # 以后就可以在自己的小金库中存放私房钱,即总金额=200+100=300
member2.share_money += 400 # 将公有账号作为自己的私有账号,并存入400元,即总金额=1000+500+400=1900
print(Family.share_money) # 1000+500=1500
print(member1.share_money) # 200+100=300
print(member2.share_money) # 1000+500+400=1900
"""
可变数据类型做静态属性的影响:
Family.native_place = 'america'
# member1.native_place[0] = 'america' # 修改的是类中的native_place,会影响所有对象(同上)
# member2.native_place[0] = 'america' # 修改的是类中的native_place,会影响所有对象(同上)
print(member1.__dict__)
print(member2.__dict__)
print(Family.__dict__)
{'role': 'father', 'name': 'lilei', 'salary': 1000, 'share_money': 300}
{'role': 'mother', 'name': 'zhanghua', 'salary': 500, 'share_money': 1900}
{'__module__': '__main__', '__doc__': '\n 定义一个公共账号 ,只要有人上班,就将钱存到这个账号上\n ',
'share_money': 1500, 'native_place': ['america'], '__init__': <function Family.__init__ at 0x0000021C360084C8>,
'work': <function Family.work at 0x0000021C3629C048>, '__dict__': <attribute '__dict__' of 'Family' objects>,
'__weakref__': <attribute '__weakref__' of 'Family' objects>}
"""
"""
可变数据类型做静态属性的影响:
member1.native_place = 'america' # 重新赋值,在当前对象的命名空间中添加这个属性,不会影响其它对象
print(member1.__dict__)
print(member2.__dict__)
print(Family.__dict__)
{'role': 'father', 'name': 'lilei', 'salary': 1000, 'share_money': 300, 'native_place': 'america'}
{'role': 'mother', 'name': 'zhanghua', 'salary': 500, 'share_money': 1900}
{'__module__': '__main__', '__doc__': '\n 定义一个公共账号 ,只要有人上班,就将钱存到这个账号上\n ',
'share_money': 1500, 'native_place': ['china'], '__init__': <function Family.__init__ at 0x000002E4747684C8>,
'work': <function Family.work at 0x000002E4749FC048>, '__dict__': <attribute '__dict__' of 'Family' objects>,
'__weakref__': <attribute '__weakref__' of 'Family' objects>}
"""
1 a = 1
2 b = 1
3 c = 2
4 d = a + b
5 print(" id(a) = %d\n id(b) = %d\n id(c) = %d\n id(d) = %d\n"
6 % (id(a), id(b), id(c), id(d)))
7
8 """
9 id(a) = 1461563616
10 id(b) = 1461563616
11 id(c) = 1461563648
12 id(d) = 1461563648
13 """
1 al = [1, 2, 3]
2 bl = [1, 2, 3]
3 print(" id(al) = %d\n id(bl) = %d\n" % (id(al), id(bl)))
4 al.append(4)
5 bl += [4]
6 print(" id(al) = %d\n id(bl) = %d\n" % (id(al), id(bl)))
7 print(" al:%s\n bl:%s\n" % (al, bl))
8
9 """
10 id(al) = 2353965003720
11 id(bl) = 2353964735816
12
13 id(al) = 2353965003720
14 id(bl) = 2353964735816
15
16 al:[1, 2, 3, 4]
17 bl:[1, 2, 3, 4]
18 """
class Family:
'''
定义一个公共账号 ,只要有人上班,就将钱存到这个账号上
'''
share_money = 0 # 不可变数据类型做静态属性
native_place = ['china'] # 可变数据类型做静态属性
def __init__(self, role, name, salary):
self.role = role
self.name = name
self.salary = salary
def work(self):
Family.share_money += self.salary # 将每个人的钱都存放到这个公共账号上
print('the account remains ¥%s '%Family.share_money)
def fun(self):
pass
class NewFamily(Family):
new_account = 0
def __init__(self, role, name, salary, kind):
super(NewFamily, self).__init__(role, name, salary)
self.kind = kind
def work(self):
pass
# 使用__定义私有属性
# python中不存在严格的私有属性,在类的外部可通过正真的函数名【_类名__函数名,即 _NewFamily__expenditure】间接调用
def __expenditure(self):
pass
f = Family('father', 'lilei', 1000)
nf = NewFamily("son", "liwei", 2000, "salesman")
print("-"*20, "nf.__dict__ 与 f.__dict__ 对比", "-"*20)
print(f.__dict__)
print(nf.__dict__)
print(set(nf.__dict__) - set(f.__dict__))
print("-"*20, "NewFamily.__dict__ 与 Family.__dict__ 对比", "-"*20)
print(Family.__dict__)
print(NewFamily.__dict__)
print(set(NewFamily.__dict__) - set(Family.__dict__))
print("-"*20, "dir(nf) 与 dir(f) 对比", "-"*20)
print(dir(f))
print(dir(nf))
print(set(dir(nf)) - set(dir(f)))
print("-"*20, "dir(NewFamily) 与 dir(Family) 对比", "-"*20)
print(dir(Family))
print(dir(NewFamily))
print(set(dir(NewFamily)) - set(dir(Family)))
"""
-------------------- nf.__dict__ 与 f.__dict__ 对比 --------------------
{'role': 'father', 'name': 'lilei', 'salary': 1000}
{'role': 'son', 'name': 'liwei', 'salary': 2000, 'kind': 'salesman'}
{'kind'}
-------------------- NewFamily.__dict__ 与 Family.__dict__ 对比 --------------------
{'__module__': '__main__', '__doc__': '\n 定义一个公共账号 ,只要有人上班,就将钱存到这个账号上\n ', 'share_money': 0, 'native_place': ['china'], '__init__': <function Family.__init__ at 0x000001EC8159D288>, 'work': <function Family.work at 0x000001EC8159D318>, 'fun': <function Family.fun at 0x000001EC8159D3A8>, '__dict__': <attribute '__dict__' of 'Family' objects>, '__weakref__': <attribute '__weakref__' of 'Family' objects>}
{'__module__': '__main__', 'new_account': 0, '__init__': <function NewFamily.__init__ at 0x000001EC8159D438>, 'work': <function NewFamily.work at 0x000001EC8159D4C8>, '_NewFamily__expenditure': <function NewFamily.__expenditure at 0x000001EC8159D558>, '__doc__': None}
{'_NewFamily__expenditure', 'new_account'}
-------------------- dir(nf) 与 dir(f) 对比 --------------------
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'fun', 'name', 'native_place', 'role', 'salary', 'share_money', 'work']
['_NewFamily__expenditure', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'fun', 'kind', 'name', 'native_place', 'new_account', 'role', 'salary', 'share_money', 'work']
{'_NewFamily__expenditure', 'new_account', 'kind'}
-------------------- dir(NewFamily) 与 dir(Family) 对比 --------------------
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'fun', 'native_place', 'share_money', 'work']
['_NewFamily__expenditure', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'fun', 'native_place', 'new_account', 'share_money', 'work']
{'_NewFamily__expenditure', 'new_account'}
"""