面向对象的日常笔记
1.
# Author:Jesi # Time : 2018/12/29 16:33 class Base(object): def func(self): super(Base,self).func() print("Base.func") class Bar(object): def func(self): print("Bar.func") class Foo(Base,Bar): pass obj = Foo() obj.func() print(Foo.__mro__) # super不是找父类,而是根据__mro__ ''' Bar.func Base.func (<class '__main__.Foo'>, <class '__main__.Base'>, <class '__main__.Bar'>, <class 'object'>) ''' # ======================================= obj = Base() obj.func() # AttributeError: 'super' object has no attribute 'func'
2.偏函数
# Author:Jesi # Time : 2018/12/29 16:23 import functools def index(a1, a2): return a1 + a2 # 偏函数帮助开发者自动传递参数 new_func = functools.partial(index, 666) ret = new_func(1) print(ret) # 667
3.继承
# Author:Jesi # Time : 2018/12/29 16:27 class Base(object): def func(self): print("base.func") class Foo(Base): def func(self): # 方式一:执行父类的func super(Foo,self).func() # 方式二:主动执行Base类的方法 Base.func(self) # 执行真正的子类func print("Foo.func") obj = Foo() obj.func()
梦想随心,天地随行