Home > Backend Development > Python Tutorial > When to use classes in python

When to use classes in python

silencement
Release: 2019-06-21 13:37:17
Original
4910 people have browsed it

When to use classes in python

All data in Python are objects. It provides many advanced built-in data types, which are powerful and easy to use. This is one of the advantages of Python. So when to use custom classes? For example, when designing a Person class, if you do not use a custom class, you can do this:

person=['mike', 23, 'male']  #0-姓名, 1-年纪, 2-性别
print(person[0], person[1], person[2])
Copy after login

As you can see, when using the built-in type list, you need to use subscripts to reference member data, which is not intuitive. You can use the dic type instead:

person1={'name':'mike', 'age': 23, 'sex': 'male'}
person2={'name':'hellen', 'age': 20, 'sex': 'female'}
print(person1['name'], person1['age'], person1['sex'])
Copy after login

This way you don’t have to remember the subscripts and it’s much more intuitive. But the syntax of the dictionary is still a bit cumbersome, and it would be better if it could be referenced like this: person.name, person.age, etc. This is the benefit of the existence of custom classes:

class Person:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    def __str__(self): #重载该函数便于测试
        sep = ','
        return self.name+sep+str(self.age)+sep+self.sex
person1 = Person('mike', 23, 'male') 
person2 = Person('hellen', 20, 'female')
print(person1)
print(person2.name, person2.age, person2.sex)
Copy after login

As you can see, as long as the constructor of this class is defined, instances of this class can be easily generated, and it is also convenient to reference data members, such as It is much more convenient to use built-in types directly. In fact, Python uses the built-in type dic to implement the storage and reference of members of custom classes. From this perspective, custom classes are a simplified way of using built-in classes, and built-in types are necessary internal components of custom types. part. At the same time, because custom classes can define their own member functions or overload predefined methods, custom classes extend the functions of built-in classes and can provide better simulations of real things. This is the advantage of object-oriented programming. . When programming, first form a concept of the thing to be simulated, and then try to use classes to capture the concept. This is the key to object-oriented design. If you need to generate multiple objects of the same type, you should design a custom class to abstract them as much as possible.

Don’t overdo the use of custom classes. Some functions only need to be defined by a function. At this time, there is no need to design a custom class.

The above is the detailed content of When to use classes in python. 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