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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| """ 前面介绍了动态的给对象增加方法 如果要所有实例对象都添加方法 只需要通过类添加方法 """
class Cat: def __init__(self, name): self.name = name
def walk_func(self): print("the name is :", self.name)
d1 = Cat('ha') d2 = Cat('bai') Cat.walk = walk_func d1.walk() d2.walk() ''' __solts__属性(元组) 可以限制该类的实例对象能够动态增加的属性和方法 1.对象有效 对类无效 类可以增加方法 2.只对本类有效 对派生的子类无效 '''
class Dog: __slots__ = ('walk', 'age', 'name')
def __init__(self, name): self.name = name
def test(self): print("the test method")
t = Dog('the_dog') from types import MethodType
t.walk = MethodType(lambda s: print('the name is %s' % s.name), t) t.walk() t.age = 22 print(t.age)
''' 用type()创建类 class创建类的时候 类型都是type 可以理解为class创建类时 解释器创建了个type对象并赋值给该类 ''' print(type(t)) print(type(Dog))
def fn(self): print("this is fn method")
Person = type('Person', (object,), dict(walk=fn, age=30))
p = Person() print(p.age) p.walk() print(type(p)) print(type(Person))
''' metaclass 是某一批类具有某种特征 动态修改类 需要继承type 重写__new__()方法 '''
class MyMetaclass(type): def __new__(cls, name, bases, attrs): attrs['cal_price'] = lambda self: self.price * self.discount return type.__new__(cls, name, bases, attrs)
class Book(metaclass=MyMetaclass): __slots__ = ('name', 'price', '__discount')
def __init__(self, name, price): self.name = name self.price = price
@property def discount(self): return self.__discount
@discount.setter def discount(self, discount): self.__discount = discount
b = Book('python', 19) b.discount = 0.5 print(b.cal_price()) ''' 类型检测 issubclass(cls,class_or_turple) cls是否为后一个类或者元组包含的多个类中任意类的子类 isinstance(obj,class_or_turple) obj是否为后一个类或者元组包含的多个类中任意类的对象 ''' hello = 'Hello' print("isinstance('hello',str):", isinstance(hello, str)) print("isinstance(hello,object): ", isinstance(hello, object)) print("isinstance(b,Book): ", isinstance(b, Book)) print("issubclass(str,object):", issubclass(str, object)) ''' __bases__ 查看所有父类 返回元组 __subclasses__() 查看所有子类 返回列表 '''
class A: pass
class B: pass
class C(A, B): pass
print(A.__bases__) print(B.__bases__) print(C.__bases__) print(A.__subclasses__()) print(B.__subclasses__()) print(C.__subclasses__())
''' 枚举类 使用Enum列出多个枚举值创建或者继承Enum ''' import enum
Season = enum.Enum('Season', ('Spring', 'Summer', 'Fall', 'Winter'))
print(Season.Spring) print(Season.Summer.name) print(Season.Summer.value)
print(Season['Fall']) print(Season(4))
for name, member in Season.__members__.items(): print(name, member, member.value)
class Orientation(enum.Enum): EAST = "1001" WEST = "1002" SOUTH = '1003' NORTH = '1004'
def info(self): print("this value is :", self.value)
print(Orientation.EAST.value) print(Orientation.EAST.name) print(Orientation('1003')) print(Orientation['NORTH']) Orientation.WEST.info()
class Gender(enum.Enum): MALE = '男', '阳刚' FEMALE = '女', '温柔'
def __init__(self, cn_name, desc): self.__cn_name = cn_name self.__desc = desc
@property def desc(self): return self.__desc
@property def cn_name(self): return self.__cn_name
print(Gender.MALE) print(Gender.MALE.name) print(Gender.MALE.value) print(Gender.FEMALE.cn_name) print(Gender.FEMALE.desc)
|