base
类的基类
类的基类的元组
方法解析时的类的查找顺序,返回元组
class Animal: __count=100 heigtht=0 def showcount3(self): print(self.__count) class Cat(Animal): name='cat' __count=200 c=Cat() c.showcount3()
结果为100
因为子类调用了父类获取父类私有变量的方法 self.count的count作用域是在父类下的,其真正调用的self._Animal__count,而这个属性只有父类有
def printable(cls): def _print(self): print(self.content, 'decorator') cls.print = _print return cls class Document: def __init__(self, content): self.content = content class Word(Document): pass class Pdf(Document): pass @printable class PrintableWord(Word): pass print(PrintableWord.__dict__) print(PrintableWord.mro()) pw = PrintableWord('test string') pw.print() @printable class PrintablePdf(Word): pass
class Document: def __init__(self, content): self.content = content class Word(Document): pass class Pdf(Document): pass class PrintableMixin: def print(self): print(self.content, 'Mixin') class PrintableWord(PrintableMixin, Word): pass print(PrintableWord.__dict__) print(PrintableWord.mro()) pw = PrintableWord('test string') pw.print() class SuperPrintableMixin(PrintableMixin): def print(self): print('~' * 20) super().print() print('~' * 20) class SuperPrintablePdf(SuperPrintableMixin, Pdf): pass print(SuperPrintablePdf.__dict__) print(SuperPrintablePdf.mro()) spp = SuperPrintablePdf('super print pdf') spp.print()
lst = [37, 99, 73, 48, 47, 40, 40, 25, 99, 51] lst.sort() def bi_insert(lst, value): high = len(lst) low = 0 while low < high: mid = (low + high) // 2 if value > lst[mid]: low = mid + 1 else: high = mid - 1 lst.insert(low, value) bi_insert(lst, 100) bi_insert(lst, 40) print(lst)
bisect.bisect_left(a, x, lo=0, hi=len(a)) 查找在有序列表a中插入x的index,lo和hi用于指定列表的区间,默认是整个列表
如果x已经存在,在其左边插入,返回值为index
bisect.bisect_right(a, x, lo=0, hi=len(a))
与bisect_right类似,如果x已经存在,在其右边插入
bisect.insort_left(a, x, lo=0, hi=len(a))
在有序列表a中插入x
bisect.insort_right(a, x, lo=0, hi=len(a))
和insort_left函数类似,如果x已经存在,在其右边插入
习题
import bisect def get_grade(score): breakpoints = [60, 70, 80, 90] grades = 'EDCBA' return grade[bisect.bisect(breakpoints, score)] for x in (50,60,69,79,85,100): print('{} -> {}'.format(x,get_grade(x)))
返回类或者对象的所有成员名称列表。
dir()就是调用dir()
如果提供dir(),则返回属性列表,否则会尽量从dict属性中收集信息
dir()对于不同类型的对象具有不同的行为:
如果对象是模块对象,返回的列表包含模块的属性名
如果对象是类型或者类对象,返回的列表包含类的属性名,及它的基类属性名
否则,返回列表包含对象的属性名、它的类的属性名和类的基类的属性名
<,<=,==,>,>=,!= lt,le,eq,gt,ge,ne
+,-,*,/,%,//,**,divmod add,sub,mul,truediv,mod,floordiv,pow,divmod
+=,-=,*=,/=,%=,//=,**=
iadd,isub,imul,itruediv,imod,ifloordiv,ipow
一般只实现eq;lt,gt实现其一,le,ge实现其一 即可
习题 完成Point类,实现判断点相等,并完成向量加法
len()会调用此方法
bool()调用时,如果没有bool方法,则调用len,返回非0为True
迭代时调用,返回迭代器对象
in 调用,如果没实现,就调用iter遍历
实现self[key]
与getitem类似
dict类调用getitem时 key不存在,调用此方法
习题 改造购物车类成方便操作的容器类
call
在类中定义此方法后,实例可调用
习题 实现斐波那契类,对象可调用
class Fibonacci: def __init__(self): self.lst = [0, 1, 1] def __call__(self, index): if index < 0: raise Exception() self.generator(index) return self[index] def __len__(self): return len(self.lst) def __iter__(self): # yield from self.lst return iter(self.lst) def __getitem__(self, item): return self.lst[item] def generator(self, index): for i in range(len(self), index + 1): self.lst.append(self[i - 1] + self[i - 2])
contextlib.contextmanager
装饰器
习题 为函数执行计时
运行时,区别于编译时,指程序被加载到内存中执行的时候
运行时获取类型信息
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句