上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 39 下一页
摘要: 1 class Num: 2 total1 = 0 3 4 def __init__(self, num, num1): 5 self.num = num 6 self.num1 = num1 7 8 def __iter__(self): 9 return self 10 11 def __nex 阅读全文
posted @ 2020-03-09 16:21 竹石2020 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 1 class Foo: 2 '迭代器协议:对象可以调用next()方法,且当不可迭代的时候会产生stopiteration,且只能向后,不能向前。' 3 a = 10 4 5 def __iter__(self): # 根据迭代器协议定义一个__iter__()方法 6 return self 7 阅读全文
posted @ 2020-03-09 15:47 竹石2020 阅读(81) 评论(0) 推荐(0) 编辑
摘要: 1 class Foo: 2 def __call__(self, *args, **kwargs): 3 print('实例被调用 obj') 4 5 f1 = Foo() 6 f1() 7 print(Foo()) 8 输出: 9 实例被调用 obj 10 <__main__.Foo objec 阅读全文
posted @ 2020-03-09 10:05 竹石2020 阅读(90) 评论(0) 推荐(0) 编辑
摘要: 析构方法:当对象在内存中被释放时,自动触发执行 此方法一般无需定义,因为python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给python解释器执行,所以,析构函数的调用是解释器在进行垃圾回收时自动触发执行的。 1 class Foo: 2 def __init__( 阅读全文
posted @ 2020-03-09 09:49 竹石2020 阅读(143) 评论(0) 推荐(0) 编辑
摘要: 1 from lib.test import test 2 3 t1 = test() 4 print(t1.__module__) # 找出这个实例来自于哪个模块 5 print(t1.__class__) # 查看这个实例由哪个类产生 6 输出: 7 lib.test 8 <class 'lib 阅读全文
posted @ 2020-03-08 22:15 竹石2020 阅读(131) 评论(0) 推荐(0) 编辑
摘要: 获取文档注释 例如: 1 class Foo: 2 '''这是个文档注释''' 3 pass 4 f1 = Foo() 5 print(f1.__doc__) 6 输出: 7 这是个文档注释 该属性无法被继承 例如: 1 class Foo: 2 '''这是个文档注释''' 3 pass 4 cla 阅读全文
posted @ 2020-03-08 21:53 竹石2020 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 1 class Foo: 2 __slots__ = ['name', 'age'] 3 4 def test(self): 5 print(self.name) 6 7 8 f1 = Foo() 9 f1.name = 'alex' 10 print(f1.name) 11 print(f1.__ 阅读全文
posted @ 2020-03-08 21:47 竹石2020 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 1 format_dic = { 2 'y-m-d': '{0.year}-{0.mon}-{0.day}', 3 'y:m:d': '{0.year}:{0.mon}:{0.day}', 4 'dmy': '{0.day}{0.mon}{0.year}', 5 } 6 7 8 class Date 阅读全文
posted @ 2020-03-08 21:14 竹石2020 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 1 class Foo: 2 num = 3 3 4 def __init__(self, name, age): 5 self.name = name 6 self.age = age 7 8 def __str__(self): # 自己定制打印信息 9 return '新的自定制显示的方法名字 阅读全文
posted @ 2020-03-08 20:23 竹石2020 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 1 class Foo: 2 def __getitem__(self, item): 3 print('getitem') 4 return self.__dict__[item] 5 6 def __setitem__(self, key, value): 7 self.__dict__[key 阅读全文
posted @ 2020-03-08 19:47 竹石2020 阅读(283) 评论(0) 推荐(0) 编辑
上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 39 下一页