目录
1.定义
类实例化对象,对象抽象为类。




如何定义一个类:
class 类名:
pass
如何通过类,创建一个对象:
class Person:
pass
根据这个类,创建(实例化)一个对象
p1=Person()
#新式类
#定义一个类
class Money:
pass
print(Money.__name__)
Money
xxx=Money
print(xxx.__name__)
Money
#Money=666
#print(Money)
#666
#id(Money)
#3284558917232
#根据这个类,创建(实例化)一个对象
one=Money()
print(one)
<__main__.Money object at 0x000002FCBF0F68C0>
one.__class__
<class '__main__.Money'>
类的定义会占用内存,Money是类的名称,也是一个变量名,类不管怎么赋值,类的类名就是Money;
Money是变量,所以可以改,Money引用着当前这个类(Money),Money=666,改的是Money变量的指向,而不是改的类名;

2.属性


1.实例属性
1.添加
1>直接通过对象,动态添加:对象名.对象属性=值;
2>通过类的初始化方法(构造方法): __init__方法
2.查询
1>对象名.对象属性
2>查看对象的所有属性:对象.dict
#定义一个类
>>> class Person:
pass
#根据类,创建一个对象
>>> p=Person()
给p对象 增加一些属性
>>> p.age=18
>>> p.height=180
验证是否增加成功(查询)
>>> p.age
18
>>> p.__dict__
{'age': 18, 'height': 180}

3.修改
对象名.对象属性=值
#修改
>>> p.age=123
>>> p.age
123
>>> p.pets=["小花","小黑"]
>>> print(p.pets,id(p.pets))
['小花', '小黑'] 2467556003720
>>> p.pets=[1,2]
>>> print(p.pets,id(p.pets))
[1, 2] 2467556009224
>>> p.pets=["小花","小黑"]
>>> print(p.pets,id(p.pets))
['小花', '小黑'] 2467556008264
>>> p.pets.append("小黄")
>>> print(p.pets,id(p.pets))
['小花', '小黑', '小黄'] 2467556008264



4.删除
del 对象名.对象属性
>>> p.age=18
>>> del p.age
>>> print(p.age)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
print(p.age)
AttributeError: 'Person' object has no attribute 'age'

5.不同对象之间不能互相访问对方的属性
>>> class Person:
pass
>>> p1=Person()
>>> p2=Person()
>>> p1.age=18
>>> p2.address='上海'
>>> print(p1.address)
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
print(p1.address)
AttributeError: 'Person' object has no attribute 'address'

2.类属性
万物皆对象,类也是一个对象
1.添加
1> 类名.类属性=值
>>> class Money:
pass
>>> Money.count=1
>>> print(Money.count)
1
>>> print(Money.__dict__)
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Money' objects>, '__weakref__': <attribute '__weakref__' of 'Money' objects>, '__doc__': None, 'count': 1}
2>
class Dog:
dogCount=0
>>> class Money:
age=18
count=1
num=666
>>> print(Money.age,Money.count,Money.num)
18 1 666
>>> print(Money.__dict__)
{'__module__': '__main__', 'age': 18, 'count': 1, 'num': 666, '__dict__': <attribute '__dict__' of 'Money' objects>, '__weakref__': <attribute '__weakref__' of 'Money' objects>, '__doc__': None}

2.查询
1>
通过类访问:类名.类属性
查看一个类的所有属性:类名.dict
>>> class Money:
age=18
count=1
num=666
>>> print(Money.age,Money.count,Money.num)
18 1 666
>>> print(Money.__dict__)
{'__module__': '__main__', 'age': 18, 'count': 1, 'num': 666, '__dict__': <attribute '__dict__' of 'Money' objects>, '__weakref__': <attribute '__weakref__' of 'Money' objects>, '__doc__': None}
2>
通过对象访问:对象名.类属性
为什么可以通过对象访问到类属性?
和python对象的属性查找机制有关
优先到对象自身查找属性,找到就结束;
若未找到,则根据__class__找到对象对应的类,到这个类里面查找
>>> class Money:
age=18
count=1
num=666
>>> one=Money()
>>> print(one.__class__)
<class '__main__.Money'>
#未找到,则根据__class__找到对象对应的类,到这个类里面查找
>>> print(one.age,one.count,one.num)
18 1 666

>>> class Test:
sex='男'
>>> class Money:
age=18
count=1
num=666
>>> one=Money()
>>> print(one.__class__)
<class '__main__.Money'>
>>> one.__class__=Test
>>> print(one.__class__)
<class '__main__.Test'>
>>> print(one.age)
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
print(one.age)
AttributeError: 'Test' object has no attribute 'age'
>>> print(one.sex)
男
#优先到对象自身查找属性,找到就结束
>>> one.sex='女'
>>> print(one.sex)
女


3.修改
1>通过类名改:类名.类属性=值
>>> class Money:
age=18
count=1
num=666
>>> Money.age=22
>>> print(Money.age)
22

2>不能通过对象改
自己操作本身,有就修改,没有就新增
>>> class Money:
age=18
count=1
num=666
>>> one=Money()
>>> one.xxx=999
>>> print(one.xxx)
999
>>> print(Money.age)
18
>>> one.age=777
>>> print(Money.age)
18
>>> print(one.__dict__)
{'xxx': 999, 'age': 777}


4.删除
1>通过类名删除:del 类名.类属性
>>> class Money:
age=18
count=1
num=666
>>> one=Money()
>>> print(Money.age,one.age)
18 18
>>> del Money.age
>>> print(Money.age)
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
print(Money.age)
AttributeError: type object 'Money' has no attribute 'age'
>>> print(one.age)
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
print(one.age)
AttributeError: 'Money' object has no attribute 'age'
2>不能通过对象删除,del语句只删除直系属性
>>> class Money:
age=18
count=1
num=666
>>> one=Money()
>>> del one.age
Traceback (most recent call last):
File "<pyshell#92>", line 1, in <module>
del one.age
AttributeError: age
5.类属性的内存存储问题
一般情况下,属性存储在__dict__的字典中,有些内置对象没有__dict__属性
>>> class Money:
pass
>>> one=Money()
>>> one.age=18
>>> one.height=180
>>> print(one.__dict__)
{'age': 18, 'height': 180}
>>> one.age=19
>>> print(one.__dict__)
{'age': 19, 'height': 180}

一般对象可以直接修改__dict__属性
>>> class Money:
pass
>>> one=Money()
>>> one.__dict__={"name":"Sz","age":18}
>>> print(one.name)
Sz

>>> class Money:
age=18
name="Sz"
>>> one=Money()
>>> one.age=18
>>> one.height=180
>>> one.__dict__["age"]=999
>>> print(one.age)
999
类对象的__dict__为只读,默认无法修改,可以通过setattr方法修改
>>> class Money:
age=18
name="Sz"
>>> print(Money.__dict__)
{'__module__': '__main__', 'age': 18, 'name': 'Sz', '__dict__': <attribute '__dict__' of 'Money' objects>, '__weakref__': <attribute '__weakref__' of 'Money' objects>, '__doc__': None}
>>> Money.__dict__={"sex": '男'}
Traceback (most recent call last):
File "<pyshell#113>", line 1, in <module>
Money.__dict__={"sex": '男'}
AttributeError: attribute '__dict__' of 'type' objects is not writable
>>> Money.__dict__["age"]=20
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
Money.__dict__["age"]=20
TypeError: 'mappingproxy' object does not support item assignment

6.类属性被各个对象共享
>>> class Money:
age=18
name="Sz"
>>> one=Money()
>>> two=Money()
>>> print(one.age,two.age)
18 18
>>> Money.age=20
>>> print(one.age,two.age)
20 20
3.对象属性和类属性之间的区别联系
1>存储地点不一样,类属性存储在类里面,对象属性存储在对象里面;
2>抽象层次不一样,一个是类,一个是根据这个类创建出来的对象,类的抽象层次更高;
3>类属性和对象属性的宿主不一样,类属性的宿主是类,对象属性的宿主是对象,
因为宿主不同,导致类属性和对象属性的操作不同,对象属性的增删改查必须通过对象,
类属性的增删改查必须通过类访问;
新增查询有些不同:
新增不同,新增类属性可以直接写在类里面,
查询不同,类属性可以通过对象来访问
>>> class Person:
age=10
>>> p=Person()
#p.age=p.age+5 这是一个赋值语句,先算右边的,
#右边的p.age相当于查询,查询到p.age=10,则p.age=10+5,这一步相当于新增p对象自己的属性
>>> p.age+=5
>>> print(Person.age)
10
>>> print(p.age)
15
4.如何限制一个对象添加属性?
通过类属性__slots__,只有在slots列表中有的,Person类的对象才能添加该属性。
>>> class Person:
__slots__=['age']
>>> p1=Person()
>>> p1.age=1
>>> p1.num=2
Traceback (most recent call last):
File "<pyshell#141>", line 1, in <module>
p1.num=2
AttributeError: 'Person' object has no attribute 'num'
>>> p2=Person()
>>> p2.age=8
>>> p2.num=9
Traceback (most recent call last):
File "<pyshell#144>", line 1, in <module>
p2.num=9
AttributeError: 'Person' object has no attribute 'num'