自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(7)
  • 收藏
  • 关注

原创 @property

class Student(object): ''' 把一个getter方法变成属性,只需要加上@property就可以了, 此时,@property本身又创建了另一个装饰器@score.setter, 负责把一个setter方法变成属性赋值 ''' @property def score(self): ...

2020-03-26 21:55:57 127

原创 __slot__

from types import MethodType class Student(object): __slots__ = ('name', 'age', 'score', 'set_score') ''' __slots__方法限制实例的属性 仅对当前实例起作用,对于子类不起作用, 除非在子类中也定义__slots__方法 这样,子类实例允许定义的属性就是自...

2020-03-26 21:55:27 96

原创 map/reduce

''' 利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。 输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']: ''' def normalize(name): return(name[0].upper() + name[1:].lower()) # 测试: L1 = ['adam', 'LI...

2020-03-26 21:54:45 73

原创 sorted

#假设我们用一组tuple表示学生名字和成绩: L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] #请用sorted()对上述列表分别按名字排序: # -*- coding: utf-8 -*- L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] def ...

2020-03-26 21:54:03 88

原创 装饰器

import functools def func(text): if isinstance(text, str): # 判断是否带参数 def decorator(log): @functools.wraps(log) # 给wrapper函数继承被装饰的函数的__name__属性 def wrapper(*args...

2020-03-26 21:52:36 63

原创 偏函数

''' 简单总结functools.partial的作用就是, 把一个函数的某些参数给固定住(也就是设置默认值), 返回一个新的函数,调用这个新函数会更简单。 ''' import functools int2 = functools.partial(int, base=2) print(int2('10000')) # 注意到上面的新的int2函数,仅仅是把base参数重新设定默认值为2,但...

2020-03-26 21:52:02 114

原创 过滤器

# #在一个list中,删掉偶数,只保留奇数,可以这么写: # def is_odd(n): # return n % 2 == 1 # print(list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))) # # 结果: [1, 5, 9, 15] # # #把一个序列中的空字符串删掉,可以这么写: # def not_empty(s): # ...

2020-03-26 21:51:11 76

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除