Members in a class
1. Fields
1. Static fields that access static fields through the class itself are created when the class code is loaded, without waiting for the new class name ()
2. Normal field access by object
2. Methods
All methods belong to the class
1. The normal method can only create an object of the class and then invoke the
Def show: Self is the class object that automatically passes the calling method
Print (Self.name)
2. Static methods the invocation of a static method does not depend on any object, called by the class name. Method Name () to save memory.
A static method is a Python function, and the parameter does not need to be added to self. Any parameter
@staticmethod
Def f1 ():
Pass
3. Class method (special form of static method)
@classmethod
Def f1 (CLS): CLS is the class name where this method is automatically passed
Pass
3. Properties
Contains fields and methods in a class in two forms
@property//attribute declaration//Method declaration
def all_page (self): def all_page (self):
Return 100
@all_page. Setter All_page must be a function defined below the adorner @property
def all_page (Self,value):
Pass
Obj.all_page = 100//Automatic call Setter Adorner the following method
@all_page. deleter
def all_page (self):
Pass
Del obj.all_page//auto-call Deleter Adorner below method
The Obj.all_page property calls the same as a field to invoke the return value of the directly fetched function
The Obj.all_page () method calls the method that must be followed by the ()
Foo = property (fget=func1,fset=func2,fdel=func3)
Foo = property (func1,func2,func3)
Func1,func2,fun3 are all methods defined in a class
The class name. Foo program executes the Func1 method and gets the return value of the FUNC1
Class name. Foo = "NewValue" Executes the Func2 method
Member modifiers
Default fields and methods name is public if you add __ in front of the field and method then __name this field becomes a private member.
A private member cannot access a private member of the parent class unless the class itself can access it
Special members of the class
Obj=foo () invokes the __init__ built-in method in the Foo class while creating the Obj object
obj () foo () () calls the __call__ built-in method in the Foo class, and this syntax exists only in Python
__str__ function:
obj = Pager () print (obj) #<__main__. Pager Object at 0x00000000006e3b00>
obj = Pager () print (obj.__dict__) #{' name ': ' 333 '} The output value is dict type data
Define a def __str__ (self) in the pager class: Return "{name:obj.name}"
Print (obj) #{name:333} outputs the object's string information instead of the object's memory address
__dict__ function:
(obj is an object) obj.__dict__ all data encapsulated by any object into Dict
(Pager is a class) PAGER.__DICT__ Convert member information contained in any class to Dict
__getitem__ (Self,item) __setitem (self,key,value) __delitem__ (self,key) function:
obj = Foo ()
obj[' AA '] #自动调用Foo类中的__getitem__
obj[' AA ']=22 #自动调用Foo类中的__setitem__
del obj[' AA '] #自动调用Foo类中的__delitem__
__iter__ function
An object can not be iterated by default, and if the class that contains the object defines the __ITER__ function, then this object can iterate
Isinstance ret=isinstance (obj,foo) See if obj is an object of the parent class of the Foo class or Foo
Issubclass Ret=issubclass (Foo,bar)
Functional differences between super and adorner (for code extensions)
1. The adorner can be used to add their own code in the function execution flow that someone else originally wrote, but the function that needs to be in the source code file
Add an adorner syntax to the sugar.
2. The super and reflection mechanism allows you to add a subclass to inherit the original written class without completely modifying the original code,
The subclass custom method adds the super (CLS). Func to add the original function to the existing code. Implements the code function extension.
Model = __import__ (path,fromlist=true) Path is a read configuration item string
CLS = GetAttr (model,classname) ClassName is a string of read configuration items
obj = CLS ()
OBJ.F1 ()
Dynamic invocation of a specific function in a Python class by means of a string.
This article from "Number One" blog, declined reprint!
Python Object-oriented