Private variables and private methods for Python classes

Source: Internet
Author: User
Tags class definition

#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2017/11/08 8:46#@Author: Lijunjiang#@File: class3.py"""The private and private methods of a class can be defined in Python by adding a double underscore to the property variable name, a special variable named 1, a variable with a _xx that begins with a single underscore, and a protected type. 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__ The definition is a special column method. 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 that are defined in Python) here, the private variables are emphasized, and Python's default member functions and member variables are public, and there are no modifiers such as public,private in other similar languages. 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 property __dict__: A class's properties (consisting of a dictionary that consists of the data properties of the Class) __doc__: The document string for the 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 of the parent classes of the class make up elements (containing a tuple of all the parent classes)"""classpub (): _name='variables of type protected'    __info='variables of private type'    def_func (self):Print("This is a protected type of method")    def __FUNC2(self):Print('This is a private type of method')    defGet (self):return(self.)__info) A=Pub ()Print(A._name) a._func ()#print (a.info)#Execution Result:#variables of type protected#This is a protected type of method#variables and methods of the protected type can be obtained and invoked in an instance of the class## Print (a.__info)## A.__FUNC2 ()#Execution Result:#File "d:/python/class/class3.py", line $, in <module>#print (a.__info)## Attributeerror:pub instance has no attribute ' __info '#A.__func2 ()#Attributeerror:pub instance has no attribute ' __func2 '#variables and methods of private types are fetched and not called in instances of the class#get a variable of a private typePrint(A.get ())#execution Result: variable of private type#If you want to get the private class deformation of a class in an instance, you can get the method by declaring the normal method in the class, returning the private class-shape variable.Print(dir (a))#execution results: [' __doc__ ', ' __module__ ', ' _func ', ' _name ', ' _pub__func2 ', ' _pub__info ', ' get ']Print(A.__dict__)#execution result: {}Print(A.__doc__)#execution Result: NonePrint(A.__module__)#execution Result: __main__Print(A.__bases__)#Execution Result:#print (a.__bases__)#Attributeerror:pub instance has no attribute ' __bases__ '

Private variables and private methods 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.