what are python classes

little bottle
Release: 2019-05-31 09:57:58
Original
21917 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. Next article I will take you to learn what "class" is.

what are python classes

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  #类体
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 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
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 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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!