Python class and Singleton mode

Source: Internet
Author: User

Python also has object-oriented thinking, then everything is Object

A class is defined in Python:

Class Student:

count = 0

Books = []

def __init__ (Self,name,grade)

Self.name=name

Self.grade=grade

def ex (self):

If self.grade== ' 1 ':

print ' AAA '

Else

print ' CCC '

In the defined class, self is equivalent to this in Java, which is equivalent to the instance itself

Access control Through "_" and "__" in the Python class

Data Properties

In the student class above, "Count" "Books" "Name" and "age" are all referred to as data properties of the class, but they are divided into class data properties and instance data properties

"_": A variable of type protected that begins with a single underscore, that is, it can only be accessed by itself and the subclass, and also represents a weak internal variable, such as when using "from modulenmae import *", an object that begins with an underscore is not introduced.
"__": a double underline represents a variable of a private type. You can only allow access to the class itself, not even subclasses, which add a single underscore and a class name to the runtime property name.

Inherited:

Supports both single-inheritance and multi-inheritance in Python

Python class and Singleton mode

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.