Object oriented members

Keywords: Python Attribute

I. General estimate

2. Composition of class members in object-oriented

1, fields

Contains static fields (one for each object) and normal fields (different data for each object)

class Person():
    company='neusoft'#Static field
    def __init__(self,name,age,gender):
        self.N=name #General field
        self.A=age
        self.G=gender
#Execute static field
print(Person.company)#Static fields called by class
#Access common fields
r=Person('tina',18,'female')
print(r.A)#Normal fields are called by objects

 

2, method

Including static methods (@ staticmethod, content that does not need to be encapsulated by objects), class methods, and ordinary methods (using data in objects)

class Person():
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def f1(self):#Common method
        print(1111)
    @classmethod#Class method
    def f2(cls):
        print(2222)
    @staticmethod#Static method
    def f3():#Static methods have no default parameters (any parameters can be added)
        print(2222)
#Perform common methods:
ret=Person('tina',18)
ret.f1()
#Call class method:
Person.f2()
#Call static method:
Person.f3()

 

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;

For example: when calling obj 1. Modify(), it is equivalent to assigning an object to a method: dbcontrl. Modify (obj 1), whose trigger is an object!

 

Class method: called by class; at least one cls parameter; when executing a class method, automatically copy the class calling the method to cls;

The trigger of a class method is a class. When a class is triggered, it passes itself as a parameter to cls = class.

Class methods are rarely used!

 

Static method: called by class; no default parameter;

In fact, you use static methods in the class: class + static method is actually equal to a function!

Trigger is a class, which is often used in a tool class. It does not need to be instantiated when calling! For example, there is a tool class. If a normal method needs to be instantiated every time it is called, if there are many people calling it, does it need to be instantiated every time? Waste memory space, so you don't need to instantiate static methods when you call them!
Summary of three methods

 

3, characteristics

Also known as property or common property (@ property ' forges a method into a field)

In python, the use of attributes is very few, but in other languages, attributes are used very much, because attributes are only available later in python, and its function is not very powerful at present! But he wants to tell people that I also support attributes, so I have this attribute!

Property is a variation of method. If there is no property in python, method can completely replace its function.

The meaning of property existence is that when accessing a property, it can create the same illusion as accessing a field.

Basic use of attributes:

# ############### 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

 

#Supplementary contents of features:
class Foo:
    def func(self):
        print('func')
    @property
    def pp(self):
        return 123
    @pp.setter
    def pp(self,value):
        print(value)
    @pp.deleter
    def pp(self):
        print('del')
obj=Foo()
print(obj.pp)
obj.pp=9999
del obj.pp

#Another way to use features:
class Foo():
    def f1(self):
        return 'sdkdlalg'
    def f2(self,value):
        print(value)
    def f3(self):
        print('lsfjsljsl')
    ZO=property(fget=f1,fset=f2,fdel=f3,doc='Here is the description')
obj=Foo()
ret=obj.ZO
obj.ZO=122324
del obj.ZO

 

2. How to quickly determine when to execute with classes and when to execute with objects

Personal summary:

If you encounter a normal ready to use object, you can use other classes. (that is, ordinary methods, fields and properties are called by objects; class methods, static fields and methods are called and executed by classes)

Teacher summary:

With self, object call; without self, class call

III. member modifier

Member modifiers include public & Private

Here is the main point

IV. special members of the class

Posted by jackson4me90 on Sat, 19 Oct 2019 19:41:54 -0700