Private fields
Class Foo:
def __init__ (self, name):
Self.__name = Name
def F1 (self):
Print (Self.__name)
Class Bar (Foo):
def f2 (self):
Print (Self.__name)
obj = Foo (' Alex ')
#print (Obj.name)
#只能内部调用
OBJ.F1 ()
Obj2 = Bar (' aaa ')
OBJ2.F1 ()
#私有普通字段只有自己能访问, the rest is not.
OBJ2.F2 ()
Static methods
Class Foo:
__CC = ' 123 '
def __init__ (self, name):
Self.__name = Name
def F1 (self):
Print (Self.__name)
@staticmethod
def f2 ():
Print (FOO.__CC)
# obj = Foo (' aaa ')
# OBJ.F2 ()
FOO.F2 ()
__getitem,__setitem,__delitem
Class Foo:
def __init__ (self, name):
Self.__name = Name
def F1 (self):
Print (Self.__name)
def __getitem__ (self, item):
Print (Type (item))
Return 123
def __setitem__ (self, Key, value):
Print (' SetItem ')
def __delitem__ (self, key):
Print (' del item ')
obj = Foo (' Alex ')
#obj [] Execute __getitem__ method
#语法对应关系
ret = obj[' ad ']
Print (ret)
Ret1 = Obj[1:2]
Print (RET1)
obj[' k1 '] = 123
Del obj[' K1 ']
Slice
Class Foo:
def __init__ (self, name):
Self.__name = Name
def F1 (self):
Print (Self.__name)
def __getitem__ (self, item):
Print (item, type (item))
Print (Item.Start)
Print (Item.stop)
Print (Item.step)
Return 123
def __setitem__ (self, Key, value):
#key. Start key.stop key.step
Print (Type (key), type (value))
def __delitem__ (self, key):
Print (Type (key))
# Key.start key.stop key.step
Print (' del item ')
obj = Foo (' Alex ')
#obj [] Execute __getitem__ method
#语法对应关系
#ret = obj[' ad ']
Ret1 = Obj[1:4:2]
Obj[1:4] = [11, 22, 33, 44, 55]
Del Obj[1:4]
Iterators
Class Foo:
def __iter__ (self):
Yield 1
Yield 2
obj = Foo ()
For item in obj:
Print (item)
#1. obj can be looped and must be iterated
The __iter__ method is executed by default when the For loop executes #2.
Isinstance and Issubclass
#isinstance: Instance of obj, Foo (parent class of obj type and obj type)
#issubclass: Whether the child class
Class Foo:
Pass
Class Bar (Foo):
Pass
obj = Bar ()
ret = isinstance (obj, Foo)
Print (ret)
Ret2 = Issubclass (Bar, Foo)
Print (Ret2)
Super
#假设有一大堆代码, it is now necessary to add functionality without changing the original code structure based on the addition of class
#然后执行super方法, super actively executes the method of the parent class
Class C1:
def F1 (self):
Print (' C1.f1 ')
Class C2 (C1):
def F1 (self):
#主动去执行父类的f1方法
Super (C2, self). F1 ()
Print (' C2.f1 ')
#不建议使用该方法
#C1. F1 (self)
obj = C2 ()
OBJ.F1 ()
On the basis of not changing the original frame, add the method
Frame structure:
Test
Backend
commons.py
index.py
lib.py (self-expanding class)
settings.py
commons.py
Class Foo:
def F1 (self):
Print (' Foo.f1 ')
settings.py
# Path = ' backend.commons '
# ClassName = ' Foo '
Path = ' lib '
ClassName = ' Myfoo '
lib.py
From backend.commons import Foo
Class Myfoo (Foo):
def f2 (self):
Print ("before")
Super (Myfoo, self). F1 ()
Print ("after")
index.py
From Settings Import ClassName
From Settings Import Path
def execute ():
#print (ClassName)
#反射获取字符串
Model = __import__ (Path, Fromlist=true)
CLS = GetAttr (model, ClassName)
obj = CLS ()
OBJ.F2 ()
if __name__ = = ' __main__ ':
Execute ()
Ordered dictionaries
Class Mydict (Dict):
def __init__ (self):
Self.li = []
Super (mydict, self). __init__ ()
def __setitem__ (self, Key, value):
Self.li.append (Key)
Super (mydict, self). __setitem__ (key, value)
def __str__ (self):
Tmp_list = []
For key in Self.li:
Value = Self.get (key)
Tmp_list.append ("'%s ':%s"% (key, value))
Tmp_str = "{" + ",". Join (Tmp_list) + "}"
Return TMP_STR
obj = Mydict ()
obj[' k1 '] = 123
obj[' K2 '] = 345
Print (obj)
Day8 Object-oriented (supplemental)