Python之Private, protected and public

Private, protected and public in Python

In C++ and Java, things are pretty straight-forward. There are 3 magical and easy to remember access modifiers, that will do the job (public, protected and private). But there’s no such a thing in Python. That might be a little confusing at first, but it’s possible too. We’ll have look at how do it right in Python.

 

Public

All member variables and methods are public by default in Python. So when you want to make your member public, you just do nothing. See the example below:

 1 class Cup:
 2     def __init__(self):
 3         self.color = None
 4         self.content = None
 5 
 6     def fill(self, beverage):
 7         self.content = beverage
 8 
 9     def empty(self):
10         self.content = None
11 
12 
13 
14 redCup = Cup()
15 redCup.color = "red"
16 redCup.content = "tea"
17 redCup.empty()
18 redCup.fill("coffee")

All of this is good and acceptable, because all the attributes and methods are public.

 

 

Protected

Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python? The answer is – by convention. By prefixing the name of your member with a single underscore, you’re telling others “don’t touch this, unless you’re a subclass”. See the example below:

 1 class Cup:
 2     def __init__(self):
 3         self.color = None
 4         self._content = None # protected variable
 5 
 6     def fill(self, beverage):
 7         self._content = beverage
 8 
 9     def empty(self):
10         self._content = None
11 
12 
13 cup = Cup()
14 cup._content = "tea"

Same example as before, but the content of the cup is now protected. This changes virtually nothing, you’ll still be able to access the variable from outside the class.

you explain politely to the person responsible for this, that the variable is protected and he should not access it or even worse, change it from outside the class.

 

 

Private

By declaring your data member private you mean, that nobody should be able to access it from outside the class, i.e. strong you can’t touch this policy. Python supports a technique called name mangling. This feature turns every member name prefixed with at least two underscores and suffixed with at most one underscore into _<className><memberName> . So how to make your member private? Let’s have a look at the example below:

 1 class Cup:
 2     def __init__(self, color):
 3         self._color = color    # protected variable
 4         self.__content = None  # private variable
 5 
 6     def fill(self, beverage):
 7         self.__content = beverage
 8 
 9     def empty(self):
10         self.__content = None
11 
12 
13 
14 Our cup now can be only filled and poured out by using fill() and empty() methods.
15 Note, that if you try accessing __content from outside, you’ll get an error. 
16 But you can still stumble upon something like this:
17 
18 
19 redCup = Cup("red")
20 redCup._Cup__content = "tea"

 

 

 

The aricle is from:

Private, protected and public in Python

Thanks the writer every much!!!

 

posted @ 2018-04-29 23:13  寒杰士  阅读(1098)  评论(1编辑  收藏  举报