摘要: class Point(object): __slots__ = ('name','point') p1 = Point() p1.name = 100 print(p1.name)#100 #p1.score = 200#由于'score'没有被放到__slots__中,所以不能绑定score属性,试图绑定score将得到AttributeError的错误 #使用__slots__要... 阅读全文
posted @ 2019-05-20 08:46 周大侠小课堂 阅读(169) 评论(0) 推荐(0) 编辑
摘要: import types print(type('abc') == str)#True print(type(123) == int)#True def f1(): pass print(type(f1) == types.FunctionType)#True print(type(abs) == types.BuiltinFunctionType)#True print(type(l... 阅读全文
posted @ 2019-05-20 08:45 周大侠小课堂 阅读(758) 评论(0) 推荐(0) 编辑
摘要: class Point(object): def __init__(self,name,score): self.__name = name self.__score = score def print_data(self): print('name:%s score:%s' % (self.__name,self.__score... 阅读全文
posted @ 2019-05-20 08:43 周大侠小课堂 阅读(492) 评论(0) 推荐(0) 编辑
摘要: 文章转载自廖雪峰老师Python课程博客,仅供学习参考使用看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的。 __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让class作用于len()函数。 除此之外,Python的class中还有许多这样有特殊用途的函数,可以帮助我们定制类。 __str__ 我... 阅读全文
posted @ 2019-05-18 08:12 周大侠小课堂 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该被直接引用。 我们在模块里公开greeting()函数,而把内部逻辑用private函数隐藏起来了,这样,调用greeting()函数不用关心内部的private函数细节,这也是一种非常有用的代码封装和抽象的方法,即: 外 阅读全文
posted @ 2019-05-17 09:00 周大侠小课堂 阅读(192) 评论(0) 推荐(0) 编辑
摘要: 第1行和第2行是标准注释,第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行,第2行注释表示.py文件本身使用标准UTF-8编码; 第4行是一个字符串,表示模块的文档注释,任何模块代码的第一个字符串都被视为模块的文档注释; 当我们在命令行运行hello模块文件时,Pyt 阅读全文
posted @ 2019-05-17 08:59 周大侠小课堂 阅读(208) 评论(0) 推荐(0) 编辑
摘要: #我们在函数lazy_sum中又定义了函数f1,并且,内部函数f1可以引用外部函数lazy_sum的参数和局部变量,当lazy_sum返回函数f1时,相关参数和变量都保存在返回的函数中,这种称为“闭包(Closure)” def lazy_f1(*args): def f1(): sum = 0 for num in args: s... 阅读全文
posted @ 2019-05-16 17:05 周大侠小课堂 阅读(154) 评论(0) 推荐(0) 编辑
摘要: import functools#functools.partial就是帮助我们创建一个偏函数的,functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单 int1 = functools.partial(int,base=2) print(int1('101010101'))#341 print(int1('123... 阅读全文
posted @ 2019-05-16 17:05 周大侠小课堂 阅读(126) 评论(0) 推荐(0) 编辑
摘要: arr1 = [1,2,3,-30,4,5,-6] arr2 = sorted(arr1)#sorted()函数就可以对list进行排序 arr3 = sorted(arr1,key=abs)#可以接收一个key函数来实现自定义的排序,例如按绝对值大小排序 #print(arr2)#[-30, -6, 1, 2, 3, 4, 5] #print(arr3)#[1, 2, 3, 4, 5, -6,... 阅读全文
posted @ 2019-05-16 17:04 周大侠小课堂 阅读(367) 评论(0) 推荐(0) 编辑
摘要: arr = [1,2,3,1,1,3,6,9] def f1(x): if x > 2: return True arr1 = list(filter(f1,arr)) print(arr1)#[3, 3, 6, 9] arr3 = ['a','b','C','',None,'123'] def f2(x):#过滤删除一个序列中的空字符串和None return... 阅读全文
posted @ 2019-05-16 17:03 周大侠小课堂 阅读(177) 评论(0) 推荐(0) 编辑