You get to know a text with python in what is an instance method, class method what is and what is a static method! ! !

Examples of methods of the class

What is an instance method of a class ?
Listen to the name can be self-evident only after being instantiated, it can be called, can be achieved only method body. General salient features will be with the self argument, because this is a unique instance method, why do you say? Because the instantiated instances of the memory address will pass in through the self, to achieve the purpose of the call instance attribute.
Code Example :

class Student(object):
    def __init__(self,name):
        self.name=name
    def information(self):
   	
        print("我叫{}".format(self.name))
		
Student("张三").information()


Student("李四").information() 
'''
运行结果:

我叫张三
我叫李四
'''

Secret what we can call the procedure :
After the first thing we look at the first instance of a class, Joe Smith will pass initialization method ( the init () method) inside the parameter name and address of this instance of the initialization method also passed the self parameter, in other words can be said to be self object instance reference variable parameter argument, the method performs initialization statements. Finished and call information () method, passing the same or instantiate address, change parameter self argument, execution method statement, calling an end.

So after the second instance of the class with the first one is pretty much the same, just after the instantiation memory address is not the same here no longer explain the.

Scenario: Examples of methods used more often, almost everywhere.

Static methods of the class

What is a static method of the class ?
Then the method with self is an instance of class methods, the method did not take self is what is it? He is a static method of the class.

Define a static method of ways :
using modifiers staticmethod, python define what needs to add modifiers @, which should have already become accustomed.

Scenario:
generally the case for instance not require access to or instance method is invoked, the class does not need to access attributes or instance method is invoked, this situation exists.
In fact, you should have seen early example:

class foo:
    def instantiation_mathod(self):
    	#不访问实例属性/类属性
        print("这是实例方法")
        
foo().instantiation_mathod()
'''
运行结果:

这是实例方法
'''

Summary : Haha! This is where you finally know the original this is the scenario ah! This is not an instance method it? Yes, this is an instance method, but he did not carry out mass participation, it can be defined as a static method. As defined in the code pycharm, pycharm you will be prompted to define a static method. So do not be instantiated, called directly.
Code Example :

class foo:
    @staticmethod
    def static_mathod():
    	#不访问实例属性/类属性
        print("这是静态方法")

foo.static_mathod()

# 不推荐使用
foo().static_mathod()

'''
运行结果:

这是静态方法
这是静态方法
'''

Conclusion : Different note with examples of methods that do not pass the self argument, can not be instantiated directly by the class name method name () call, it can () method name invoked by class name through the instantiated, but this.. calls lost meaning, if instantiated, calling the method, then the significance of what Shen Mingcheng static methods do?

Class Methods

What is a class method ?
Class method after instantiating instances are running, then if
necessary to use a class method.
The method defined class manner :
add a decorative device in the above method is modified @classmethod

Usage scenario :
When a method involves only static property class methods may be used:
Code Example :

class fruit():
    __discount=1
    def __init__(self,name,price):
        self.name=name
        self.__price=price
    def price(self):
        return "水果名:{},价格:{}/斤".format(self.name,self.__price*self.__discount)

    @classmethod
    def fruit_discount(cls,new_discount):
       cls.__discount=new_discount

fruit.fruit_discount(0.8)
print(fruit("苹果", 18).price())
print(fruit("香蕉",20).price())

'''
运行结果:

水果名:苹果,价格:14.4/斤
水果名:香蕉,价格:16.0/斤
'''

Conclusion : In the above case, we will set off to redefine a class, and add a decorator @classmethod, you can not call after instantiated. Some people may not understand that the class methods useless. Because class methods can influence an instance method, which means that only one call class methods, instance methods discounts already changed.
So these three what difference does it?

Examples of the difference method, class methods, static methods:

These three methods are stored in the class of memory, different callers.
Examples of the method called by the object, at least one self parameter representative of self reference object.
Class by the class method calls, at least one parameter cls, and requires modification decorator @classmethod
static method call by a class, no parameters need decorator modified @staticmethod

To wave, push it!
Group number: 781 121 386
group name: Life is short, I learned programming
welcome to join us, along with the exchange of technology !!!

Published 38 original articles · won praise 128 · views 10000 +

Guess you like

Origin blog.csdn.net/lujiangyang123/article/details/103658914