1. field
Field: it includes normal field and static field. They are different in definition and use, but the most essential difference is the location saved in memory.
Common fields belong to objects
Static fields belong to class
Definition and use of fields:
class Province: # Static field country = 'China' def __init__(self, name): # General field self.name = name # Direct access to common fields obj = Province('Hebei Province') print obj.name # Direct access to static fields Province.country
From the above code, it can be seen that common fields need to be accessed through objects, and static fields need to be accessed through classes. In terms of use, it can be seen that the ownership of common fields and static fields are different. The storage mode of the content is similar to the following figure:
It can be seen from the above figure:
Only one copy of static field is saved in memory
Common fields need to be saved in each object
Application scenario: when creating objects from a class, if each object has the same field, the static field is used
2. method
Methods include: ordinary method, static method and class method. All three methods belong to the class in memory, and the difference is that they are called in different ways.
Normal method: called by an object; at least one self parameter; when the normal method is executed, the object calling the method is automatically assigned to self;
Class method: called by class; at least one cls parameter; when executing a class method, automatically copy the class calling the method to cls;
Static method: called by class; no default parameters.
Definition and use of methods:
class Foo: def __init__(self, name): self.name = name def ord_func(self): """ Define common methods, at least one self parameter """ # print self.name print 'Common method' @classmethod def class_func(cls): """ Define class methods, at least one cls parameter """ print 'Class method' @staticmethod def static_func(): """ Define static method, no default parameters""" print 'Static method' # Call normal method f = Foo() f.ord_func() # Call class method Foo.class_func() # Call static method Foo.static_func()
Same point: all methods belong to class (non object), so only one copy is saved in memory.
Different points: different method callers, different parameters passed in automatically when calling methods.
3. attribute
If you already know the methods in Python classes, the properties are very simple, because the properties in Python are actually variations of ordinary methods.
For attributes, there are three knowledge points:
Basic use of properties
Two ways to define attributes
3.1 basic use of attributes
################ Definition ############### class Foo: def func(self): pass # Defining attributes @property def prop(self): pass # ############### call ############### foo_obj = Foo() foo_obj.func() foo_obj.prop #Call attribute
The following points should be paid attention to by the definition and call of properties:
When defining, add @ property decorator on the basis of common methods;
When defined, the property has only one self parameter
When calling, no bracket is required
Method: foo obj. Func()
Property: foo obj.prop
Note: the meaning of an attribute is that when accessing an attribute, you can create the same illusion as when accessing a field. The attribute is derived from a method variation. If there is no attribute in Python, the method can completely replace its function.
Example: for the host list page, it is impossible to display all the contents of the database on the page in each request. Instead, it is displayed locally through the paging function. Therefore, when requesting data from the database, all the data (i.e. limit m,n) from item M to item n will be obtained according to the specified display. The paging function includes:
Calculate m and n based on the current page and total number of data requested by the user
Request data from database according to m and n
# ############### Definition ############### class Pager: def __init__(self, current_page): # Page number currently requested by the user (first, second...) self.current_page = current_page # Each page displays 10 pieces of data by default self.per_items = 10 @property def start(self): val = (self.current_page - 1) * self.per_items return val @property def end(self): val = self.current_page * self.per_items return val # ############### call ############### p = Pager(1) p.start Is the starting value, that is: m p.end Is the end value, that is: n
It can be seen from the above that the function of Python's attributes is to perform a series of logical calculations inside the attributes, and finally return the calculation results.
3.2 two ways to define attributes
There are two ways to define attributes:
Decorators: applying decorators to methods
Static field: define a static field in the class with a value of property object
3.2.1 decorator mode
Decorator mode: apply @ property decorator to common methods of class
We know that there are classic and new classes in Python, and the properties of new classes are richer than those of classic classes. (if the class follows object, it is a new class)
Classic class with a @ property decorator (example in the previous step)
# ############### Definition ############### class Goods: @property def price(self): return "wupeiqi" # ############### call ############### obj = Goods() result = obj.price # Automatic execution @property Decorated price Method and get the return value of the method
New class with three @ property decorators
# ############### Definition ############### class Goods(object): @property def price(self): print '@property' @price.setter def price(self, value): print '@price.setter' @price.deleter def price(self): print '@price.deleter' # ############### call ############### obj = Goods() obj.price # Automatic execution @property Decorated price Method and get the return value of the method obj.price = 123 # Automatic execution @price.setter Decorated price Method and assign 123 to the parameters of the method del obj.price # Automatic execution @price.deleter Decorated price Method
Note: there is only one access method for the properties in the classic class, which corresponds to the method decorated by @ property
There are three ways to access the properties in the new class, and they correspond to three methods decorated by @ property, @ method name. setter, @ method name. Delete
Because there are three access methods in the new class, we can define three methods to the same property: get, modify and delete according to their access characteristics
Example:
class Goods(object): def __init__(self): # Original price self.original_price = 100 # Discount self.discount = 0.8 @property def price(self): # Actual price = Original price * Discount new_price = self.original_price * self.discount return new_price @price.setter def price(self, value): self.original_price = value @price.deltter def price(self, value): del self.original_price obj = Goods() obj.price # Get commodity price obj.price = 200 # Modify the original price of goods del obj.price # Delete original price of goods
3.2.2 static field mode
Static field method: create a static field with the value of property object
When using static fields to create properties, there is no difference between classic and new classes
class Foo: def get_bar(self): return 'wupeiqi' BAR = property(get_bar) obj = Foo() reuslt = obj.BAR # Automatic call get_bar Method and get the return value of the method print reuslt
There are four parameters in the property construction method:
The first parameter is the method name. The execution method will be triggered automatically when calling the object and property
The second parameter is the method name. The execution method will be triggered automatically when calling object. Property = XXX
The third parameter is the method name. The execution method will be triggered automatically when calling del object. Property
The fourth parameter is the string, which calls the object. Property. doc. This parameter is the description information of the property
class Foo: def get_bar(self): return 'wupeiqi' # *Two parameters are required def set_bar(self, value): return return 'set value' + value def del_bar(self): return 'wupeiqi' BAR = property(get_bar, set_bar, del_bar, 'description...') obj = Foo() obj.BAR # Automatically call the method defined in the first parameter: get_bar obj.BAR = "alex" # Automatically call the method defined in the second parameter: set_bar Method, and“ alex"Passed in as a parameter del Foo.BAR # Automatically call the method defined in the third parameter: del_bar Method obj.BAE.__doc__ # Get the value set in the fourth parameter automatically: description...
Because there are three access ways to create attributes in static field mode, we can define three methods to the same attribute: get, modify and delete according to their access characteristics
Example:
class Goods(object): def __init__(self): # Original price self.original_price = 100 # Discount self.discount = 0.8 def get_price(self): # Actual price = Original price * Discount new_price = self.original_price * self.discount return new_price def set_price(self, value): self.original_price = value def del_price(self, value): del self.original_price PRICE = property(get_price, set_price, del_price, 'Price attribute description...') obj = Goods() obj.PRICE # Get commodity price obj.PRICE = 200 # Modify the original price of goods del obj.PRICE # Delete original price of goods
Therefore, there are two ways to define attributes: decorator and static field, while decorator is different for classic and new types.