Pseudo-Private Properties | Pseudo-Private Methods | Python

Source: Internet
Author: User

1. Private Properties#double underline before attribute            #to protect property security            #a private property cannot be called directly through an object, and a calling method needs to be added to invoke it;            classpeople (object):def __init__(self): self.__name='Mic'                                defget_private_attr (Self, new_name): Self.__name=new_namePrint(self.)__name)            #Creating Objectsp =people ()#Get private PropertyP.get_private_attr ('Lily')            Print(p.__name)        2. Private Methods#Similarly, calls require another method to invoke a private method;            classpeople (object):def __init__(self): self.__name='Mic'                                def __get_name(self):Print(self.)__name)                defget_name (self): self.__get_name() P=people ()#P.__get_name ()P.get_name ()------------------------------------------------->>>Pseudo-Private#!! In fact, the Python class is not permission control, the so-called double underline is private, in fact, can be accessed;            classPrivate (object):def __inaccesible(self):Print('you don't have access to')                definaccesible (self):Print('Actually, you can see that.') self.__inaccesible() P=Private ()#p.__inaccesible ()            #p.inaccesible ()p._private__inaccesible ()>>>you don't have access toclassPrivate (object):def __init__(self): self.__name='Mic'                defget_name (self):Print(self.)__name) P=Private ()#print (p.__name)            #P.get_name ()            Print(P._private__name)>>>'Mic'

Pseudo-Private Properties | Pseudo-Private Methods | Python

Related Article

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.