Private methods and private properties in Python

Source: Internet
Author: User

1. Private methods and private properties
Private methods can only be called inside a class and cannot be used by objects
Private properties can only be used inside a class and cannot be used by objects

Private properties can only be used inside a class, and objects cannot be used, but we may call or modify private properties by defining a public method inside the class, and then the object is used in calling this public method.
# # #私有属性和私有方法 ########
#在属性名和方法名前面加上 __

class Person(object):    def __init__(self):        # 公有属性        self.name = "李四"        #私有属性        self.__age = 18    #定义公有方法    def show_info(self):        # 私有属性只能在类内部使用,可以通过内部公有方法修改,        self.__age = 29        print(self.name, self.__age)#调用私有方法

#在类内部使用私有方法, can be called through an internal public method
result = Self.__show_address ()
Print (Result)
#定义私有方法
def __show_address (self):
Return "My home is in the Northeast"

#定义对象, call the class

person = Person()person.show_info()

#测试对象调用私有属性, result error, object cannot use private property, private property can only be used internally
#print (Person.name, person. Age
#测试对象调用私有方法, the result is an error, the private method can only be used inside the class
#person.
Show_address ()
Output:

Lee 429
My home is in the northeast.

===================================================================
Extensions: Private properties and private methods, just disguise the name, change it to another name, cause our access to fail,
If we get the name after the disguise, the object can still call private methods and private properties outside the class

Private in Notice::python not absolute private (unconventional method)

#查看对象属性
dict (you can use objects or classes to view objects or class properties)

Through print (person. Dict) Gets the property name of the object person

#查看对象属性和方法
Dir (Can view object or class properties and methods)

See the properties and method names of the object person through print (dir)

When you see the method and the property name after the disguise, the object can call

2. Subclass inherits parent class, default cannot use private methods and properties of parent class

====================
Extended:
However, if we get the name of the parent class private property and the private method, the subclass can also invoke the

3. Modification of private properties

Private properties can only be modified inside the class
Let the latter object call the public method by defining a public method modification

=====================
Extended:
If we know the name of the private property after masquerading, we can also modify the private property by the name of the disguise.

We are outside the object by invoking the private property name to modify, not the real modification, but the object is added to the beginning of the property, and did not modify the private properties

Summary: Private methods and private properties can only be used inside a class, and changes to private properties can only be modified within the class (except for unconventional operations)

Private methods and private properties in Python

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.