Home >Backend Development >Python Tutorial >The difference between Python class methods and instance methods

The difference between Python class methods and instance methods

(*-*)浩
(*-*)浩Original
2019-06-29 09:27:066843browse

The more common method types in Python are instance methods and class methods. How are they defined? How to call it? What are their differences and functions? See below.

The difference between Python class methods and instance methods

#First of all, these methods are defined in the class. Let me first briefly talk about how to define and call it. (PS: Instance objects have the greatest permissions.)

Instance method (Recommended learning: Python video tutorial)

Definition: first parameter It must be an instance object. The parameter name is generally agreed to be "self", through which the attributes and methods of the instance can be passed (the attributes and methods of the class can also be passed);

Call: can only be called by the instance object.

Class method

Definition: Use the decorator @classmethod. The first parameter must be the current class object. The parameter name is generally agreed to be "cls", through which the attributes and methods of the class are passed (the attributes and methods of the instance cannot be passed);

Call: instance object and Class objects can be called.

Instance methods

In short, instance methods are methods that can be used by instances of a class. Not much explanation here.

Class method

Use the decorator @classmethod.

In principle, class methods are methods that operate on the class itself as an object. Suppose there is a method, and it is logically more reasonable for this method to be called using the class itself as an object, then this method can be defined as a class method. In addition, if inheritance is required, it can also be defined as a class method.

Suppose I have a student class and a class class. The functions I want to achieve are:

Perform the operation of increasing the class size and obtain the total number of class members;

Students The class inherits from the class class. Every time a student is instantiated, the number of students in the class can increase;

Finally, I want to define some students and get the total number of people in the class.

class ClassTest(object):
    __num = 0

    @classmethod
    def addNum(cls):
        cls.__num += 1

    @classmethod
    def getNum(cls):
        return cls.__num

    # 这里我用到魔术方法__new__,主要是为了在创建实例的时候调用累加方法。
    def __new__(self):
        ClassTest.addNum()
        return super(ClassTest, self).__new__(self)


class Student(ClassTest):
    def __init__(self):
        self.name = ''

a = Student()
b = Student()
print(ClassTest.getNum())

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between Python class methods and instance methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn