Home > Article > Backend Development > what are python classes
In this article, let’s learn about the knowledge about classes. Some friends may have just come into contact with the programming language python and don’t understand what python classes mean, but it doesn’t matter. Next article I will take you to learn what "class" is.
1. Definition of python class
Class (Class): used to describe the same attributes and methods A collection of objects. It defines the properties and methods common to every object in the collection. Objects are instances of classes.
2. How to create a class
Use the class statement to create a new class. After class is the name of the class and ends with a colon:
class ClassName: '类的帮助信息' #类文档字符串 class_suite #类体
The help information of the class can be viewed through ClassName.__doc__.
class_suite consists of class members, methods, and data attributes.
3. Python creates class instance
The following is an example of a simple Python class:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Employee: '所有员工的基类' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salary
(empCount variable is a class variable, it The value will be shared among all instances of this class. You can access it using Employee.empCount in the inner class or the outer class.
The first method __init__() method is a special method, called It is called the constructor or initialization method of the class. This method will be called when an instance of this class is created.
self represents the instance of the class. self is required when defining the method of the class. Although it is called when There is no need to pass in the corresponding parameters.)
The above is all the content described in this article. This article mainly introduces the knowledge of python classes. I hope you can use the information to understand what is said above and the examples given. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
The above is the detailed content of what are python classes. For more information, please follow other related articles on the PHP Chinese website!