Home  >  Article  >  Backend Development  >  What is class in python

What is class in python

silencement
silencementOriginal
2019-06-27 09:22:4313098browse

What is class in python

There are two important concepts in the object-oriented programming process: class (class) and object (object, also known as instance, instance), where a class is a certain batch of The abstraction of objects can be understood as a certain concept; the object is a concrete entity. In this sense, what we call people in daily life are actually human objects, not human beings.

The simple syntax for defining a class in Python is as follows:

class 类名:
    执行语句...
    零个到多个类变量...
    零个到多个方法...

The class name only needs to be a legal identifier, but this only meets the grammatical requirements of Python: if it is readable from the program From a gender perspective, Python's class names must be concatenated by one or more meaningful words. The first letter of each word is capitalized, and the other letters are all lowercase. Do not use any separators between words.

Judging from the above definition, Python's class definition is a bit like a function definition. They all start with a colon (:) as the class body and use the uniformly indented part as the class body. The difference is that function definitions use the def keyword, while class definitions use the class keyword.

Python's class definition consists of a class header (referring to the class keyword and class name part) and a uniformly indented class body. The two most important members in the class body are class variables and methods. If no class variables and methods are defined for the class, then the class is equivalent to an empty class. If the empty class does not require other executable statements, the pass statement can be used as a placeholder. For example, the following class definition is allowed:

class Empty:
    pass

Generally speaking, empty classes do not have much practical significance.

The definition order of the members in the class has no effect, and the members can call each other.

The two most important members of a Python class are variables and methods. The class variables belong to the class itself and are used to define the state data contained in the class itself: while the instance variables belong to the objects of the class. , used to define the state data contained in the object: methods are used to define the behavior or function implementation of the object of this class.

Python is a dynamic language, so the class variables contained in its classes can be dynamically added or deleted (when the program assigns values ​​to new variables in the class body, it adds class variables), and the program can also add class variables anywhere. Add variables to existing classes; the program can delete class variables of existing classes through the del statement.

Similarly, the instance variables of Python objects can also be dynamically added or deleted (as long as a new instance variable is assigned a value, the instance variable is added), so the program can add instance variables to its own objects anywhere; the program Instance variables of existing objects can be deleted through the del statement.

The methods defined in the class are instance methods by default. The method of defining instance methods is basically the same as the method of defining functions, except that the first parameter of the instance method will be bound to the caller of the method (the class instance), so instance methods should define at least one parameter, which is usually named self.

Note: The first parameter of the instance method does not have to be called self. In fact, it can be called any parameter name. It is just a convention to name the parameter self, which has the best readability.

There is a special method in the instance method: __init__. This method is called the constructor method. The constructor is used to construct an object of this class, and Python returns an object of this class by calling the constructor (no need to use new).

Many methods in Python that start with a double underscore and end with a double underscore have special meanings. These special methods will be introduced in detail later in this tutorial.

The constructor is the fundamental way for a class to create objects, so Python also provides a function: if the developer does not define any constructor for the class, then Python will automatically define a constructor for the class that contains only one self. The default constructor for parameters.

The following program will define a Person class:

class Person :
    '这是一个学习Python定义的一个Person类'
    # 下面定义了一个类变量
    hair = 'black'
    def __init__(self, name = 'Charlie', age=8):
        # 下面为Person对象增加2个实例变量
        self.name = name
        self.age = age
    # 下面定义了一个say方法
    def say(self, content):
        print(content)

The above Person class code defines a construction method. The construction method only has a special method name: __init__, the first part of the method One parameter is also self, bound to the object initialized by the constructor.

The above is the detailed content of What is class in python. 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