The private variables and private methods of the overridden classes of the Python class

Source: Internet
Author: User
Tags class definition

1. Overriding the class

Example:

class parent (object):    name =  ' Parent '     age = 100    def __init__ (self):         print (' my name is parent ')     def  Get_name (self):        return self.name     Def get_age (self):         return self.ageclass child ( Parent):    name =  "Child"     def __init__ (self):         print (' my name is {0} '. Format (self.name))      def hello (self):         print (' hello {0} '. Format (Self.name))     a = child () A.hello () print (A.get_name ()) print (A.get_age ()) 

In the above example, if you feel that the method of the parent class is not good, and want to write a new method,

We rewrite the Get_name method in the subclass to get rid of the method of the parent class, how to do it?

Class parent (object):    name =  ' parent '     age =  100    def __init__ (self):         print (' My name is parent ')     def get_name (self):         return self.name    def get_age (self):         return self.ageclass child (parent):     name =   ' child '     def __init__ (self):         Print (' my name is {0} '. Format (self.name))     def hello (self):         print (' hello {0} '. Format (self.name))     def  get_name (self):        print  (' nice day! ')     a&nbsP;= child () A.hello () A.get_name () print (A.get_age ()) 

Results:

My name is child

Hello Child

Nice day!

100

This overrides the method of the class.


In another case, __init__ in the initialization of the class, generally do not inherit, the above parent class and the child class inside the two __init__ do not inherit each other

Class initialization, whether it is the parent class or subclass, when initialized, each class only go their own __init__;

What if the subclass needs to execute __init__ in the parent class at initialization time?

Or the same example:

Class parent (object):    name =  ' parent '     age =  100    def __init__ (Self, address, sex):         self.address = address         Self.sex = sex        print (' my name is  Parent ')     def get_name (self):         return  self.name    def get_age (self):         Return self.ageclass child (parent):    name =  "Child"      def __init__ (self):         print (' my name  is {0} '. Format (self.name))     def hello (self):         print (' hello {0} '. Format (self.name))     def get_name (self):         print (' nice day! ') A = child () A.hello () A.get_name () print (A.get_age ())

In the above example, if the child class wants to inherit address and sex from __init__ in the parent class, use the Super () method

Class parent (object):    parent_name =  ' parent '     age  = 100    def __init__ (Self, address, sex):         self.address = address         Self.sex    = sex        print (' My name  is parent ')     def get_name (self):         return self.name    def get_age (self):         return self.ageclass child (parent):    child_name =  "Child"     def __init__ (self, address, sex):         super (child, self)  = __init__ (address,sex)          print (' my name is {0} '. Format (self.name))     def hello (self):         print (' hello {0} '. Format (self.name))     def  Get_name (self):         print (' nice day! ')


2. Private variables and private methods for classes

In Python, you can define a property as a private property by adding a double underscore to the property variable name

Special variable naming

1. _xx, which begins with a single underscore, is a variable of type protected. That is, the protection type can only allow access to its own and subclasses. If an internal variable is marked, for example, when "from M import" is used, an object that begins with an underscore is not introduced.


2, __xx double underline is a variable of the private type. Only the class itself can be accessed, and the subclass cannot be used to name a class attribute (class variable), and the name is changed when called (Within the class FooBar, __boo becomes _foobar__boo, such as Self._foobar__boo)


3. __xx__ defines the method of the special column. Variables or attributes within a user-controlled namespace, such as Init, __import__, or file. Do not define such variables yourself unless the document is described. (that is, these are the variable names defined within Python)


Here we emphasize the private variables, the Python default member function and member variables are public, there is no such as other similar language public,private and other keywords decorated. However, you can add two underscore "_" to the variable before In this case, the function or variable becomes private. This is Python's private variable rolling (this translation is very awkward), English is (private name mangling.) * * The case is when the variable is marked as private, insert the class name in front of the variable, and then add an underscore "_" before the class name. The _classname__ variable name is formed. * *


Python built-in class properties

__DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)

__DOC__: Document string for Class

__MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)

__BASES__: All parent classes of a class make up elements that contain a tuple of all the parent classes


This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1979780

The private variables and private methods of the overridden classes of the Python class

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.