摘要:
# __next__和__iter__实现迭代器协议 class Foo: def __init__(self, n): # 初始化 self.n = n def __iter__(self): # 定义类的__iter__方法,实例化时就为一个可迭代对象 return self def __next__(self): # 定义类的_... 阅读全文
posted @ 2018-08-13 21:17
四十不惑的编程之路
阅读(506)
评论(0)
推荐(0)
摘要:
# __slots__ __doc__ __module__ __class__ __del__ __call__ # __slots__ 定义在类中的类属性,如果你有一个属性很少的类,但是有很多实例,为了节省内存可以使用__slots__ # 在类当中定义了__slots__,实例就没有自己的属性字典__dict__只能使用__slots__提供的属性且不能新增其它属性 # 实例查看属性时不... 阅读全文
posted @ 2018-08-13 20:01
四十不惑的编程之路
阅读(125)
评论(0)
推荐(0)
摘要:
# __getitem__ __setitem__ __delitem__ # 只适用于类似操作字典的操作才会触发 class Foo: def __getitem__(self, item): print('调用了__getitem__') return self.__dict__[item] def __setitem__(self, key... 阅读全文
posted @ 2018-08-13 20:00
四十不惑的编程之路
阅读(150)
评论(0)
推荐(0)
摘要:
# __str__ __repr__ 与 __format__ class Foo: def __init__(self, year, month, day): self.year = year self.month = month self.day = day def __str__(self): return ... 阅读全文
posted @ 2018-08-13 20:00
四十不惑的编程之路
阅读(170)
评论(0)
推荐(0)
摘要:
# isinstance issubclass 与系统内置属性 __getattribute__ # isinstance(obj, cls)检查obj是否是类cls的实例 class Foo: pass obj = Foo() print(isinstance(obj, Foo)) # True 判断obj是否是Foo类的实例 mystr = 'hello world' pr... 阅读全文
posted @ 2018-08-13 00:20
四十不惑的编程之路
阅读(140)
评论(0)
推荐(0)

浙公网安备 33010602011771号