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

What is a class in python

藏色散人
藏色散人Original
2019-07-06 11:51:255467browse

What is a class in python

The most important concepts of object-oriented are class and instance. It must be remembered that class is an abstract template, such as Student class, and instance is created based on class. A specific "object", each object has the same methods, but their data may be different.

Still taking the Student class as an example, in Python, defining a class is through the class keyword:

class Student(object):
    pass

class is followed by the class name, which is Student. The class name usually starts with a capital. The word, followed by (object), indicates which class the class is inherited from. We will talk about the concept of inheritance later. Usually, if there is no suitable inheritance class, the object class is used. This is what all classes will eventually inherit. kind.

After defining the Student class, you can create an instance of Student based on the Student class. Creating an instance is achieved through the class name ():

>>> bart = Student()
>>> bart
<__main__.Student object at 0x10a67a590>
>>> Student
<class &#39;__main__.Student&#39;>

You can see that the variable bart points to For a Student object, the following 0x10a67a590 is the memory address. The address of each object is different, and Student itself is a class.

You can freely bind attributes to an instance variable. For example, bind a name attribute to the instance bart:

>>> bart.name = &#39;Bart Simpson&#39;
>>> bart.name
&#39;Bart Simpson&#39;

Since the class can function as a template, you can create an instance when When doing so, we forcefully fill in some attributes that we think must be bound. By defining a special __init__ method, when creating an instance, the name, score and other attributes are tied to it:

class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score

Note that the first parameter of the __init__ method is always self, indicating creation The instance itself, therefore, within the __init__ method, you can bind various properties to self, because self points to the created instance itself.

With the __init__ method, when creating an instance, you cannot pass in empty parameters. You must pass in parameters that match the __init__ method, but self does not need to be passed. The Python interpreter itself The instance variable will be passed in:

>>> bart = Student(&#39;Bart Simpson&#39;, 59)
>>> bart.name
&#39;Bart Simpson&#39;
>>> bart.score
59

Compared with ordinary functions, the function defined in the class has only one difference, that is, the first parameter is always the instance variable self, and when calling, there is no need to pass the parameter. Other than that, class methods are no different from ordinary functions, so you can still use default parameters, variable parameters, and keyword parameters.

Related recommendations: "Python Tutorial"

The above is the detailed content of What is a 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