What are the classes in Python? How do I create a class?

Source: Internet
Author: User
In this article let's look at the knowledge about classes, some friends may have just come into contact with Python, the programming language, for what is what does the Python class mean?I don't understand, but it's okay. The next article will take you to learn what "class" is.

I. Definition of Python class

Class: Used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.

Two. How to create a class

Use the class statement to create a new class, followed by the name of the class and ending with a colon:

Class ClassName:   ' Help information for classes '   #类文档字符串   class_suite  #类体

The Help information for the class can be viewed through classname.__doc__.

Class_suite consists of class members, methods, and data properties.

Three. Python creates class instances

The following is an example of a simple Python class:

#!/usr/bin/python#-*-Coding:utf-8-*-class Employee:   ' base class for all employees '   empcount = 0    def __init__ (self, name, Sala RY):      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:", S Elf.salary

(The Empcount variable is a class variable whose value is shared across all instances of the class.) You can use Employee.empcount access in internal classes or outside classes.

The first method, the __init__ () method, is a special method called the constructor or initialization method of a class that is called when an instance of the class is created.

Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate parameters when calling. )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.