
本文共 7507 字,大约阅读时间需要 25 分钟。
@classmethod���������������������������������������������������cls ���������������
���������������
1������������������������������������������������������
2������������������������������������������������������������������������������
������������������
1������������������������������������ self ������������ self ������������������
2������������������������������������������������������������������������������������������������������������������������
# ���������������class Goods: __discount = 0.8��� def __init__(self, original_price): self.original_price = original_price self.price = self.original_price * self.__discount��� @classmethod # ��������������������������������������������������� def change_discount(cls, count): cls.__discount = count # ���������Goods.__discount = count������Goods.change_discount(0.6) # ������������������������������������# ���������������������������������huawei = Goods(20)print(int(huawei.price))���huawei.change_discount(0.4) # ������������������������������������# ���������������������������������apple = Goods(20)print(int(apple.price))���# ������128
���������
���������������������������������������������������������
import time���class Date: def __init__(self, year, month, day): self.year = year self.month = month self.day = day��� @classmethod def today(cls): structure = time.localtime() # ������������������������������ obj = cls(structure.tm_year, structure.tm_mon, structure.tm_mday) return obj������# ������������������today���������������������������������������������������������������today������������������������data������ = Date.today()print(data������.year)print(data������.month)print(data������.day)���# ������202154
@staticmethod������������������������������������������������
��������������������������������������������������������������������������������������������� @staticmethod ���������������������
������������������������������ self ��������������������� cls ���
class User: pass��� @staticmethod def login(user): print(f'{user}���������������')���User.login('Alen') # ���������������Bob = User()Bob.login('Bob') # ������������������# ������Alen���������������Bob���������������
���������������������������������������
1������������������������������������������������������ ���������������������������
2������������������������������������������ ���������������������
3������������������������������self��������������� ���������������������
4���������������������������cls��������������� ���������������������������
5���property������������������������������������ ���������������������������������������
������������������������
__call__
��������������� + ( ) ���������������������__call__
������
class User: pass��� @staticmethod def login(user): print(f'{user}���������������')���User.login('Alen') # ���������������Bob = User()Bob.login('Bob') # ������������������# ������Alen���������������Bob���������������
__len__
���������len (������) ���������������������������__len__
������
class A: def __init__(self): self.lis = [1, 2, 3, 4, 5]��� def len(self): return len(self.lis)��� def __len__(self): return len(self.lis)������a = A()print(len(a)) # ������������len���a������������������lis������������������print(a.len()) # ���������������������������len������������������������# ������55���# -------------------------------------------------------------------------------------# ���������������������������������������������class A: def __init__(self, count): self.count = count��� def __square__(self): value = self.count ** 2 return value���def square(count): return A.__square__(count)���a = A(5)���print(square(a))���# ������25
__new__
���������������������������������������__new__
���������������������������������������������
class A: def __new__(cls, *args, **kwargs): o = super().__new__(cls) print(o) # ��������������� self ��������������������� print('���������__new__������') return o��� def __init__(self): print(self) print('���������__init__������')���A() # ������������������# ������<__main__.A object at 0x014E0970>���������__new__������<__main__.A object at 0x014E0970>���������__init__������
__new__
������������������������������������>������������
��������������������������������������������� self ���������
class Car: __attribute = None��� def __new__(cls, *args, **kwargs): if cls.__attribute is None: # ������������������������������__attribute��������������� # ������������������������������������������__attribute��������������������������������������������������� cls.__attribute = super().__new__(cls) return cls.__attribute # ���������������������self��� def __init__(self, car_paint, cartwheel_colour): self.car_paint = car_paint self.cartwheel_colour = cartwheel_colour������xiao_hong = Car('���������������', '���������������')print('������������������������', xiao_hong) # ������������������������������������print(xiao_hong.car_paint) # ���������������������������>���������������������������������������xiao_ming = Car('���������������', '���������������')print('������������������������', xiao_ming)print(xiao_hong.car_paint) # ���������������������������>������������������������������������������������print(xiao_ming.car_paint) # ���������������������������>������������������������������������������������������# ������������������������������ <__main__.Car object at 0x01A70A60>��������������������������������������� <__main__.Car object at 0x01A70A60>������������������������������
__str__
���__repr__
���������
str���������������������������������������������������������������������������������������
1������������������������������������������__str__
������
2������ %s ������������������������������������__str__
������
3������str������������������������������__str__
������
������������������������������������������������������������������������������������������������������������������������������__str__
������������������������������������������������������������������������������������������������������������������������������������������ str ���������������������������������������������������������
repr������������ str ���������������str���������������str������������������rper������������ %r ��� repr ���������������
class User: def __init__(self, name, age): self.name = name self.age = age��� def __str__(self): return self.name��� def __repr__(self): return self.age������Bob = User('������', '18')���print(Bob) # ������������������������������������������������������str������������������������������������������������������������ str ���������print('���������������%s' % Bob) # ��� %s ������������������������������������__str__������print('���������������%r' % Bob) # ��� %r ������������������������������������__repr__���������# ������������������������������������������������18
发表评论
最新留言
关于作者
