A, Member:
1. Fields: Static fields (each object has the same field), Normal dictionary (each object has different data)
2. Methods: Static methods (without using the object's contents), class methods, common methods (using data in objects)
3. Features: Common characteristics (the method is forged into a field)
Access through classes are: Static fields, static methods, class methods
Access by object: Normal field, class method
Own members to visit themselves
static method: No self, add @staticmethod before, that is static method (Access by Class)
Class method: A parameter more than a static method, which is to show which class, before adding @classmethod
Attribute: The method does not cause a field to be executed, before adding @property
Quick Judgment, class execution, object execution
The Self object is called
No self class calls
test01
class Provice: #静态字段, class country= "China" def __init__ (self,name): #普通字段, in Object Self.name=name #普通方法, Class def show (sel f): Print ("show") #静态方法 @staticmethod def Add (): Print ("add #类方法, one more parameter @classmethod def Dev ( cls ): Print ("subtraction", CLS) #特性, the method does not cause a field to execute @property def csv (self): Print ( "CSV") #设置csv中的值, 3 csv names must be the same @ csv . Setter def Csv (self,value): Print (value) Pro=provice ("Shandong") Print (provice.country) Print (pro.name) Provice.add () Print (Provice.dev ()) Print (pro.csv) pro.csv= "123456" ------------------------------- ------------------
China Shandong Addition Subtraction <class ' __main__. Provice ' > Csv 123456 |
B, use reflection to import modules, find classes, create objects, find words in objects
imp=__import__ ("test01", Fromlist=true) Print (IMP)
Class_name=getattr (Imp, "Foo") Print (class_name)
R=class_name (' Zhangyu ') Print (R) Print (GetAttr (r, ' name ')) ---------------------------------------- <module ' test01 ' from ' f:\\myworkspacedirectory\\function\\member\\test01.py ' > <class ' test01. Provice ' > <test01. Provice Object at 0x00000247fefc79e8> Zhangyu |
C, member modifier
Member:
Field: Static field (each object has the same field), Plain dictionary ( Each object has different data)
methods: Static methods (without using the contents of the object), class methods, common methods (using data in objects)
attribute: Common features (Forged methods into fields)
Two kinds: common, private (two underscore starts, only oneself can use, Derived classes also cannot access)
class Provice: #静态字段, class country= "China" #私有字段 for internal use
__country = "Eglish" def __init__ (self,name,name1): #普通字段, in Object Self.nam E=name #私有的有普通字段 self.__name1=name1 def __add (self): Print ("_ _add ") @staticmethod def __dev (): Print (" __dev ") # Normal method, Class Def show (self): br> print ("show") Print (provice.__country) self.__name1= "666" Self.__add () Self.__dev () @staticmethod def mm (): Provice.__dev provice.__name1 = "6661" Obj=provice ("Zhangyu", "nnn") Print (provice.country) #错误情况 #print (provice.__country) Obj.show () provice.mm () --------------------------------------------- China Show Eglish __add __dev |
D, special members of the Class 1
__init__
__del__
__call__===>django
XXX.__DICT__: Can view members in a class
Class Foo: ‘‘‘ I am the comment of the class ‘‘‘ country= "Zhongguo"
def __init__ (self): Self.name= "666"
def __call__ (self, *args, **kwargs): Print ("Call") Return 1
def __getitem__ (self, item): Print (Item,type (item), "__getitem__")
def __setitem__ (self, Key, value): Print (Key,value, "__setitem__")
def __delitem__ (self, key): Print (Key, "__delitem__")
def add (self): Print ("1")
# R=foo () () # print (R)
R=foo () #-------------> Execute init R () #------------------> Execution __call__ r[' K1 ' #------------------> Execute __getitem__ r[' K2 ']= ' 666 ' #------------------> Execute __setitem__ del r[' xxx '] #------------------> Execute __ __delitem__ R[1:5:2] #2.7 Execution Getslice 3.0 execution GetItem R[1:3:3]=[11,22,33,44,55,66] #2.7 Execution Setslice 3.0 execution SetItem Del R[1:3:3] #2.7 Perform Detslice 3.0 execution Detitem
Print (r.__dict__) Print (foo.__dict__) ---------------------------------------------- Pager K1 <class ' str ' > __getitem__ K2 666 __setitem__ XXX __delitem__ Slice (1, 5, 2) <class ' slice ' > __getitem__ Slice (1, 3, 3) [One, one, one, one, one, one] __setitem__ Slice (1, 3, 3) __delitem__ {' name ': ' 666 '} {' __module__ ': ' __main__ ', ' __doc__ ': ' \ n I am the comment of the class \ n ', ' Country ': ' Zhongguo ', ' __init__ ': <function foo.__init__ at 0x00 0001c307cb8950>, ' __call__ ': <function foo.__call__ at 0x000001c307cb89d8>, ' __getitem__ ': <function Foo._ _getitem__ at 0x000001c307cb8a60>, ' __setitem__ ': <function foo.__setitem__ at 0x000001c307cb8ae8>, ' __ delitem__ ': <function foo.__delitem__ at 0x000001c307cb8b70>, ' Add ': <function foo.add at 0x000001C307CB8BF8 , ' __dict__ ': <attribute ' __dict__ ' of ' foo ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' foo ' objec Ts>} |
E, ITER: Special members of the Class 2
Class Foo:
def __iter__ (self): Yield 1 Yield 2 Yield 3 Yield 4
Obj=foo () #如果执行for对象时, the ITER method that automatically executes the object, the generator For i in obj: Print (i) ---------------------------------------------------- 1 2 3 4 |
41. Python Object-oriented two