Traps for Python class private methods

Source: Internet
Author: User
Tags class definition

Introduction

Python does not define member functions or properties like C + +, Java, C #, etc. with explicit public, private, or protected keywords, which are identified using the Convention's single underscore "_" and "__" double underscores as a prefix to a function or property. There is a big difference between using a single underline or a double underline.

1. Single underline function or attribute, can be called and accessed in the class definition, the instance of the class can be accessed directly, the subclass can be accessed;

2. Double-underlined functions or properties, which can be called and accessed in the class definition, cannot be accessed directly by an instance of the class, and the subclass is inaccessible.

Note: For double-underlined functions or properties, the Python interpreter uses the method of name obfuscation, turning the private method "__method" into "_classname__method", as shown in the example below.



Private functions and properties of double underlines, not visible in subclasses, no overwrite

Class Base (object):    def __private (self):        print (' Private value in Base ')    def _protected (self):        print ( ' protected value in base ')    def public (self):        print ("Public value in base")        Self.__private ()        self._ Protected () class Derived (Base):    def __private (self):        print ("Override Private")    def _protected (self):        Print ("override protected") Print dir (Base) print ("=" *80) d = Derived () d.public () d._protected () d._derived__ Private () print ("=" *80) d.__private ()
The output results are as follows:

>>> [' _base__private ', ' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ' , ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' _protected ', ' Public ']============================================ ====================================public value in baseprivate value in Baseoverride protectedoverride Protectedoverride private================================================================================ Traceback (most recent):  File "D:\temp\test.py", line, <module>    




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Traps for Python class private methods

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.