# 经典类 没有继承 object的类
# 新式类 继承了 object的类
class Money: # 2.x中默认是经典类,3.x中是新式类
pass
class Money(object): # 兼容的一种写法
pass
# Money既是类的__name__属性名,又是一个引用该类的变量
print(Money.__name__) # Money
xxx = Money
print(xxx.__name__) # Money
one = Money()
print(one) # <__main__.Money object at 0x000001555E9534A8>
print(one.__class__) # <class ‘__main__.Money‘>
class Person:
pass
p = Person()
# 给 p对象增加属性, 所有的属性好像是以字典的形式组织的
p.age = 18
print(p.age) # 18
print(p.__dict__) # {‘age‘: 18}
print(p.sex) # AttributeError: ‘Person‘ object has no attribute ‘sex‘
# 删除p对象的属性
del p.age
print(p.age) # AttributeError: ‘Person‘ object has no attribute ‘age‘
class Money:
num = 666
count = 1
type = "rmb"
print(Money.num) # 666
# 对象查找属性,先到对象自身去找,若未找到,根据 __class__找到对应的类,然后去类中查找
one = Money()
print(one.count) # 1
# 不能通过对象去 修改/删除 对应类的属性
one.num = 555
print(Money.num) # 666
print(one.num) # 555
# 类属性会被各个对象共享
two = Money()
print(one.num, two.num) # 666 666
Money.num = 555
print(one.num, two.num) # 555 555
# 类中的 __slots__属性定义了对象可以添加的所有属性
class Person:
__slots__ = ["age"] # 只允许添加一个 age属性
p1 = Person()
p1.age = 1
p1.num = 2 # AttributeError: ‘Person‘ object has no attribute ‘num‘
Python没有真正的私有化支持,只能用给变量添加下划线来实现伪私有;通过名字重整机制
属性的访问范围:类的内部-->子类内部-->模块内的其他位置-->其他模块
公有属性 x
受保护属性 _x
私有属性 __x
保护数据案例
class Person:
def __init__(self):
self.__age = 18
def set_age(self, age): # 错误数据的过滤
if isinstance(age, int) and 0 < age < 150:
self.__age = age
else:
print("Wrong age value")
def get_age():
return self.__age
p = Person()
print(p.get_age()) # 18
p.set_age(22)
print(p.get_age()) # 22
# 1. 属性私有化 + 属性化 get()方法
class Person(object):
def __init__(self):
self.__age = 18
# 可以以使用属性的方式来使用方法
@property
def age(self):
return self.__age
p = Person()
print(p.age) # 18
p.age = 666 # Attribute Error: can‘t set attribute
# 2. 通过底层的一些函数
class Person:
# 通过 属性 = 值 的方式来给一个对象增加属性时,底层都会调用这个方法,构成键值对,存储在 __dict__字典中
# 可以考虑重写底层的这个函数,达到只读属性的目的
def __setattr__(self, key, value):
if key == "age" and key in __dict__:
print("read only attribute")
else:
self.__dict__[key] = value
class Person:
def instance_fun(self): # self: 调用对象的本身,调用时不用写,解释器会传参
print("instance method", self)
@classmethod
def class_fun(cls): # cls: 类本身
print("class method", cls)
@staticmethod
def static_fun():
print("static method")
class Person:
__age = 18
def __run(self): # 只能在该类中被调用
print("running...")
a, s = 8, "123"
print(a.__class__, s.__class__) # <class ‘int‘> <class ‘str‘>
print(int.__class__, str.__class__) # <class ‘type‘> <class ‘type‘>
type是元类。
通 type
元类来创建类,动态创建。也可以用__metaclass__
来指明元类,进行类的创建。
type
来创建类def run(self):
print("run...")
Dog = type("Dog", (), {"count": 0, "run": run})
print(Dog) # <class ‘__nain__.Dog‘>
d = Dog()
print(d.count) # 0
print(d.run()) # run...
calss Person:
def __init__(self, n, a):
self.name = n
self.age = a
# 面向用户
def __str__(self):
return "name: %s, age: %d" % (self.name, self.age)
# 面向开发人员
def __repr__(self):
# todo
# 一般默认给出对象的类型及地址信息等
# 打印或进行格式转换时,先调用 __str__()函数,若未实现,再调用 __repr__()函数
p = Person("Rity", 18)
print(p) # name: Rity, age: 18
res = str(p)
print(res) # name: Rity, age: 18
print(repr(p)) # <__main__.Person object at 0x000001A869BEB470>
# 使得一个对象可以像函数一样被调用
class PenFactory:
def __init__(self, type):
self.type = type
def __call__(self, color):
print("get a new %s, its color is %s" % (self.type, color))
pencil = PenFactory("pencil")
pen = PenFactory("pen")
# 一下两种使用方式会调用 __call__()函数
pencil("red") # get a new pencil, ites color is red
pencil("blue") # get a new pencil, ites color is blue
pen("black") # get a new pen, ites color is black
class Person:
def __init__(self):
self.cache = {}
def __setitem__(self, key, value):
self.cache[key] = value
def __getitem__(self, key):
return self.cache[key]
def __delitem__(self, key):
del self.cache[key]
p = Person()
p["name"] = "Rity"
...
# 使得自己定义的类可以按照一定的规则进行比较
import functools
@functools.total_ordering
class A:
def __init__(self, age, height):
self.age = age
self.height = height
def __eq__(self, other): # ==
return self.age == other.age
def __lt__(self, ohter): # <
return self.age < other.age
a1, a2 = A(18, 170), A(19, 178)
# 因为逻辑具有相反性,当使用 > 时,首先会查找 __gt__()函数,若未定义,将参数交换后调用 __lt()__方法
# 由 == 和 < 可以组合出其他的所有比价逻辑,使用装饰器可以自动生成其他逻辑对应的函数,简化代码
print(a1 < a2) # True
print(a2 > a1) # True
print(a1 >= a2) # False
本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
原文:https://www.cnblogs.com/chengxuyuanaa/p/12586639.html