What are classes in python? How to create a class?

乌拉乌拉~
Release: 2018-08-20 14:38:19
Original
6470 people have browsed it

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. The next article will take you to learn what “class” is.

1. Definition of python class

Class (Class): used to describe a collection of objects with the same attributes and methods. 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, followed by class is the name of the class and ends with a colon:

class ClassName:
   '类的帮助信息'   #类文档字符串
   class_suite  #类体
Copy after login

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 instances

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
Copy after login

(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 of 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.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What are classes in python? How to create a class?. 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