Home > Backend Development > Python Tutorial > How to understand python classes

How to understand python classes

(*-*)浩
Release: 2019-08-02 14:53:47
Original
5016 people have browsed it

As a newbie who has just started Python, I don’t understand the concept of classes. When should I define a class and what is the role of this class? After reading a lot of books and web pages, I can summarize it as follows:

How to understand python classes

##Class (class): class is a classification of a class in real life. An abstraction of things with common characteristics, used to describe a collection of objects with the same properties and methods.

Reference code: (Recommended learning: Python video tutorial)

# 定义“人”类
class Person(object):
    class_name = "人类"
    #初始化时需要给“人”分配一个名字name
    # 工作时长 working_time则留给“男人”和“女人”去分开定义
    def __init__(self, name):
        self.name = name
        self.working_time = None

    #定义一个方法,它能输出工作时长
    def work(self):
        print(self.working_time)
    #还可以定义其他方法

# 定义“男人”类, 它需要“继承”“人”类
class Man(Person):
    def __init__(self, name):
        # 调用“人”类的初始化方法以完成继承
        Person.__init__(self, name)
        # 定义工作时长
        self.working_time = 8

# 定义“女人”类,它需要“继承”“人”类
class Woman(Person):
    def __init__(self, name):
        # 调用“人”类的初始化方法以完成继承
        Person.__init__(self, name)
        # 定义工作时长
        self.working_time = 6

print(Person.class_name)  # 输出  人类
zhangsan = Man("zhangsan") 
print(zhangsan.working_time) # 输出 8
Lisi = Woman("Lisi")
print(Lisi.working_time)  # 输出 6
Copy after login
The first line, this is the fixed syntax of Python3. Of course, Person is the class name we named ourselves, and it is usually recommended to capitalize the first letter. (object) is also a fixed syntax.

The second line, the class_name variable is a class variable, and its value will be shared among all instances of this class.

Next, the first def, called the "constructor" or "initialization method" of the class, is actually the basic information of this class. When an "instance" of this class is created, it can be called this basic information. __init__ is also a fixed format. There are two parameters here, self and name, which correspond to "self, name". In fact, you can name it as you like. Of course, the first self is also a convention. Self here refers to "self" and "instantiation" The "self" of that person in the future.

For more Python related technical articles, please visit the

Python Tutorial column to learn!

The above is the detailed content of How to understand python classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template