Python language and the like

Source: Internet
Author: User

1. An empty class

1 # Filename:emptyclass.py 2 3 class Empty: 4     Pass 5 6 e = Empty ()7print#<__main__. Empty object at 0x022f6df0>

2. Methods of the class

The method of the class must have an extra first parameter, self, which points to the object itself. The call does not need to be assigned, and Python provides this value.

__init__ (self [, param1,param2 ...])

Run immediately when an object of the class is established.

__del__ (self)

It is called when the object dies, but it is difficult to guarantee when the method will run.

3. Inheritance

1 #Filename:inherit.py2 classFather:3     def __init__(self,name):4Self.name =name5 6 classChild (Father):7     def __init__(self,name,age):8Father.__init__(Self,name)9Self.age = Age

Python does not automatically call the constructor of the base class.

Python supports multiple inheritance . When it cannot find the method called in the current class, it is looked up in the parent class, in the order of inheritance .

4. Simulating abstract classes

Notimplementederror class

Python language and the like

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.