pythonprotect_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 classCup:2 def __init__(self):3 self.color =None4 self.content =None5

6 deffill(self, beverage):7 self.content =beverage8

9 defempty(self):10 self.content =None11

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 classCup:2 def __init__(self):3 self.color =None4 self._content = None #protected variable

5

6 deffill(self, beverage):7 self._content =beverage8

9 defempty(self):10 self._content =None11

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 _ . So how to make your member private? Let’s have a look at the example below:

1 classCup:2 def __init__(self, color):3 self._color = color #protected variable

4 self.__content = None #private variable

5

6 deffill(self, beverage):7 self.__content =beverage8

9 defempty(self):10 self.__content =None11

12

13

14 Our cup now can be only filled and poured out by using fill() andempty() methods.15 Note, that if you try accessing __content fromoutside, 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:

Thanks the writer every much!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值