Python Learning Notes--privatization __python

Source: Internet
Author: User
I. Privatization in the class

The class has private properties, private methods, external secrecy, and will not inherit from the quilt class.

1. Private properties

Let's write a class below.

Class Test (object): ""
    docstring for Test ""
    def __init__ (self):
        super (Test, self). __init__ ()
        self. __num = 100

Private property __num, below we call from the outside:

Error TIP: The test class does not have this attribute.

Our general practice at this point is to expose two methods, get Fetch value method, and set value method.
As follows:

So we have access to this property.

(laughter)
In fact, there is another way to access.
is to call directly.
In the previous direct call, why can't we call it.
It's actually because Python has secretly changed the name of the variable.

We use the dir () command to view all the methods and properties in this class. The display is as follows:

Did you see that? The marker for the arrow. PY June Change the private variable name to "_ Class name + private variable name", in my here is "_test__num". Now let's try it again.

See if there's a visit. Not only can access, but also modify it ~ (Don't mess with this method yo)

2. Private method

Class Test (object): ""
    docstring for Test ""
    def __init__ (self):
        super (Test, self). __init__ ()

    def __ Printname (self):
        print (' sy ')

t = Test ()

t.__printname ()

Run as follows:

Error TIP: The test class does not have this attribute.

Private methods are restricted to use in this class.

In essence, PY-June changed the name of the private class, let's try it with the method we just called.

This method is good to know, don't mess with OH ~ ii. privatization in Modules

Let's write a module first.

num =

_num2 =

__num3 =

def printname ():
    print (' sy ')

def _printname1 ():
    print (' Sy1 ')

def __printname1 ():
    print (' Sy2 ')

Import using from private import *

(Incidentally, review the previous import method)

As long as it is with "_", all are undefined errors.

Let's try the import private.

So you can use it.

I think the reason is that when using the * import, the Py will automatically filter out the properties and methods that start with "_".
(I use "_private__num2" method also can not be called Yo ~)

If you import the entire module directly, the module itself will be able to invoke all the properties and methods within the module, including private.

Above.

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.