1. Members of a class
The members of a class can be divided into three categories: fields, methods, and properties
Note: Among all members, only the contents of ordinary fields are stored in the object, that is, according to the number of objects created by this class, there are as many ordinary fields in memory. And other members are stored in the class,
that is: no matter how many objects, only one copy is created in memory
1. Field
Fields include: common fields and static fields, they are different in definition and use, and the most essential difference is the storage location in memory.
Common fields belong to objects
and static fields belong to classes.
class Province: # static fields country = 'China' def __init__(self, name): # normal fields self.name = name # Direct access to normal fields obj = Province('Hebei Province') print obj.name # Direct access to static fields Province.country
It can be seen from the above code that [ordinary fields need to be accessed through objects] [static fields are accessed through classes], it can be seen that the attribution of ordinary fields and static fields is different in use
Static fields save only one
common field in memory, and one
application scenario is saved in each object: when creating objects through classes, if each object has the same fields, then use static fields
2. Methods
Methods include: ordinary methods, static methods, and class methods. All three methods belong to classes in memory. The difference lies in the way they are called.
1. Ordinary method: called by the object ; at least one self parameter; when executing a common method, the object that calls the method is automatically assigned to self;
2. Class method: called by the class ; at least one cls parameter; when executing a class method, automatically Copy the class that calls this method to cls;
3. Static method: called by the class ; no default parameters;
class Foo: def __init__(self, name): self.name = name def ord_func(self): """ defines a normal method with at least one self parameter""" # print self.name print 'normal method' @classmethod def class_func(cls): """ Define a class method with at least one cls parameter""" print 'class method' @staticmethod def static_func(): """ Defines a static method with no default parameters""" print 'static method' # call normal method f = Foo() f.ord_func () # call the class method Foo.class_func() # call static method Foo.static_func()
Similarities: For all methods, they belong to classes (non-objects), so only one copy is saved in memory.
Differences: different method callers, different parameters automatically passed in when calling the method
3. Properties
Attributes in Python are actually variants of ordinary methods , which are basically used as follows:
# ###############Definition############## class Foo:
def __init__(self, name):
self.name = name def func(self): pass # define properties @property def prop(self): print self.name # ############### transfer############### foo_obj = Foo(kevin) foo_obj.func() foo_obj.prop #call property, looks like call field
As can be seen from the above, the function of Python's attributes is: a series of logical calculations are performed inside the attribute, and the calculation results are finally returned.
The following points should be paid attention to when defining and calling properties:
When defining, add the @property decorator on the basis of ordinary methods ;
when defining, when the property has only one self parameter
, no parentheses
are required. Method call: foo_obj.func()
Property call: foo_obj.prop
1. Two ways of attribute definition
1. The decorator is: apply the decorator to the method
2. The static field is: define the static field with the value of the property object in the
class. The decorator method: apply the @property decorator to the ordinary method of the class
Classic class, with a kind of @property decorator (like the example in the previous step)
New-style classes 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' # ############### transfer############### obj = Goods() obj.price # Automatically execute the price method modified by @property and get the return value of the method obj.price = 123 # Automatically execute the price method modified by @price.setter and assign 123 to the method parameter del obj.price # Automatically execute the price method modified by @price.deleter
There are three access methods for the properties in the new class, and they correspond to the three methods modified by @property, @method name.setter, and @method name.deleter.
We can separate the three methods according to the access characteristics of several of their properties. Defined for the same attribute: get, modify, delete
Static field method, create a static field whose value is a property object
When using static fields to create properties, there is no difference between classic and new-style classes
class Foo: def get_bar(self): return 'wupeiqi' BAR = property(get_bar) obj = Foo() reuslt = obj.BAR # Automatically call the get_bar method and get the return value of the method print reuslt
There are four parameters in the construction method of property:
the first parameter is the method name, which will automatically trigger the execution method when the property is called.
The second parameter is the method name, which will automatically trigger the execution method when the property = XXX
. The parameter is the method name, which is automatically triggered when the del object is called.
The fourth parameter is a string, which is the calling object. Attribute. __doc__ , this parameter is the description of the attribute
class Foo: def get_bar(self): return 'wupeiqi' # *must have two arguments 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: the set_bar method, and pass "alex" as a parameter del Foo.BAR # Automatically call the method defined in the third parameter: del_bar method obj.BAE.__doc__ # Automatically get the value set in the fourth parameter: description...
Since there are three access methods for creating properties by static field, we can define three methods for the same property according to the access characteristics of several properties: get, modify, delete
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 property description...') obj = Goods() obj.PRICE # Get the item price obj.PRICE = 200 # Modify the original price of the product del obj.PRICE # delete the original price of the item
2. Modifiers for class members
Public members, private members can be accessed anywhere
, and methods are only available within the class.
The definitions of private members and public members are different : when private members are named, the first two characters are underscores. (except special members such as: __init__, __call__, __dict__, etc.)
The access restrictions of private members and public members are different:
static fields
public static fields: the class can access; it can be accessed inside the class;
private static fields can be accessed in derived classes: only inside the class can be accessed;
class C: name = "public static field" def func(self): print C.name class D(C): def show(self): print C.name C.name # class access obj = C() obj.func() # can be accessed inside the class obj_son = D() obj_son.show() # accessible in derived classes public static fields
class C: __name = "public static field" def func(self): print C.__name class D(C): def show(self): print C.__name C.__name # class access ==> error obj = C() obj.func() # Accessible inside the class ==> Correct obj_son = D() obj_son.show() # accessible in derived classes ==> error private static fields
Common fields
Public common fields: objects can be accessed; classes can be accessed within the class;
private common fields can be accessed in derived classes: only accessible within the class;
class C: def __init__(self): self.foo = "Public Field" def func(self): print self.foo # access inside the class class D(C): def show(self): print self.foo # Access in derived class obj = C() obj.foo # access via object obj.func() # class internal access obj_son = D(); obj_son.show() # Access in derived classes public field
class C: def __init__(self): self.__foo = "private field" def func(self): print self.foo # access inside the class class D(C): def show(self): print self.foo # Access in derived class obj = C() obj.__foo # access via object ==> error obj.func() # class internal access ==> correct obj_son = D(); obj_son.show() # access ==> error in derived class private field
Note: If you want to force access to private fields, you can access it through [object._class name__private field description] (eg: obj._C__foo), it is not recommended to force access to private members
The access of methods and attributes is similar to the above method, that is: private members can only use ps inside the class : if you want to access private attributes, you can pass object._class__attribute name