Class 2 in python, python
Class (2) in python 6. Member fields of the class: common fields and static Fields
Eg:
Class Province (): country = 'China' # The static field is saved in the class. During execution, you can access def _ init _ (self, name) through the class or object ): self. name = name # common field, saved in object, can only access print (Province. country) # China, access obj = Province ('henan ') print (obj. name) # Henan, access print (obj. country) # China, access through objects
Methods: common methods, static methods, and class methods
Application scenarios:
Common method: some values need to be saved in the object. When performing a function, the values in the object need to be used.
Static Method: no value in any object is required.
Eg:
Class Foo (): def f1 (self): # A common method stored in the class, called by an object. The form parameter self indicates the called object print (123) @ staticmethod def f2 (a1): # static method. self is not a required parameter and is saved in the class. You can directly call print (a1) @ classmethod def f3 (cls) through the class ): # class method, the form parameter cls indicates the current class name, so you can access print through the class
Attribute
The attribute is actually a method, but it is not added with brackets after the method name as it is called.
Class Foo (): @ propery # used to execute obj. f4 def f4 (self): print (789) return 1 @ f4.settr # used to execute obj. f4 = 123 def f4 (self, val) print (val) @ f4.deleter # used to execute del obj. f4 def f4 (self): print (111) Call: obj = Foo () # create an object r = obj. f4 # print (r) #789 # 1obj. f4 = 123 # 123del obj. f4 #111
Member Modifier
The class members are divided into two types: A common member and a private member. A total of members can access the service directly, while a private member cannot access the service externally.
Total members:
Previously created members are all co-members.
Private member:
When creating a member, add two underscores before the member name. Although the private member cannot be directly accessed from outside, it can be accessed within the class. Private Members in the parent class, and child classes cannot be directly used.
Class Person (): def _ init _ (self, name, age): self. name = name # self. _ age = age # private member def show (): print (self. _ age) F = Person ('Tom ', 18) F. show () #18, Access F. name # tom, through external access
Special member _ call __
Class Call: def _ call _ (self, * args, ** kwargs): print ('OK') # Call method 1 F = Call () F () # execute the _ call _ method and output OK # Call method 2 call () # execute the _ call _ method and output OK
_ Str __
It is generally used in print. If this method is not added, the memory address of this class is displayed when you print the object of this class.
Eg:
Class F (): def _ init _ (self, n, a): self. name = n self. age = a def _ str _ (self): return self. name # Call A = F ('Tom ', 18) print (A) # tom, which is similar to print (str (A), str () the str method in F is automatically called and the return value is obtained.
_ Del _ (destructor)
Run the command when the object of this class is destroyed.
_ Getiterm __
Index value.
Eg:
Class Person (): def _ init _ (self, name, age): self. name = name self. _ age = age def _ getitem _ (self, item): return item + 10F = Person ('Tom ', 18) print (F [2]) #12, the _ getitem _ method in the F object class is automatically executed.
_ Setiterm __
Assign values through indexes.
Eg:
Class Person (): def _ init _ (self, name, age): self. name = name self. _ age = age def _ setitem _ (self, key, value): print (key, value) F = Person ('Tom ', 18) F [3] = 222 #3 222, automatically execute the _ setitem _ method in the F object class.
_ Iter __
If the class contains the _ iter _ method, the object can be iterated.
Object.The return value of _ iter _ () is the iterator.
When a for loop encounters an iteratable object, the object is automatically executed.._ Iter _ (): generate an iterator and perform a for loop.
Eg:
class Person(): def __init__(self, name, age): self.name = name self.__age = age def __iter__(self): return iter([11, 22, 33])F=Person('tom',18)for i in F: print(i)
Metaclass
class F1(type): def __init__(self,what,bases=None,dict=None): super(F1, self).__init__(what,bases,dict) print (1) def __call__(self): print (2) self.__new__(self) self.__init__(self)class S(object,metaclass=F1): def __new__(cls, *args, **kwargs): pass def __init__(self): print (3)
Output result:
1
2
3