python3 私有成员_Python中的私有成员

python3 私有成员

If you have prior experience with other programming languages (Java, C#, C++, Kotlin) and have leapt to learn python, you will definitely look for the private members to provide encapsulation as it is a key principle of object-oriented programming. Everyone does that until getting familiar with the “pythonic” way of coding style.

如果您具有其他编程语言(Java,C#,C ++,Kotlin)的经验,并且打算学习python,那么您肯定会寻找私有成员来提供封装,因为这是面向对象编程的关键原理。 每个人都会这样做 ,直到熟悉“ pythonic”编码风格。

Looking into the few resources over the internet I found quite interesting thoughts that are really insightful and humorous as well. You must have a look over this link which I liked most.

在浏览互联网上的一些资源时,我发现了很多有趣的想法,这些想法也很有见地和幽默。 您必须先看一下我最喜欢的链接

Python doesn’t stop you to access private and protected attributes and methods as they are public by default 😎. So, the implementation decision has been left for developers.

Python不会阻止您访问私有和受保护的属性和方法,因为默认情况下它们是公共的。 因此,实施决策留给了开发人员。

Protected

受保护的

Single Underscore ( _ ) before fields and methods are used to indicate protected access-modifier.

字段和方法之前的单个下划线(_)用于指示受保护的访问修饰符。

class User:
def __init__(self, username, password):
self._username = username # i act like a protected but you can change me
self._password = password # i act like a protected but you can change me
def _login(self):
print('i am like a protected method but you can access me')

Private

私人的

Double Underscore ( __ ) before fields and methods are used to indicate private access-modifier.

在使用字段和方法表示私有访问修饰符之前,请使用双下划线(__)。

class User:
def __init__(self, username, password):
self.__username = username # i act like a private but you can change me
self.__password = password # i act like a private but you can change me
def __login(self):
print('i am like a private method but you can access me')

Access of Private Modifier

访问私有修饰符

class Doctor:
def __init__(self,
name,
address,
specialities,
schedule,
degree):
self.name = name
self.address = address
self.specialities = specialities
self.schedule = schedule
self.__degree = degree
def __diagnose_patient(self):
print(F'diagnosed patient bob by doctor {self.name}')
if __name__ == '__main__':
doctor = Doctor('Dr. Bob', 'KTM', 'OPD', 'SUN: 7-8', 'MBBS')
doctor._Doctor__diagnose_patient()

It can be seen in the above code snippet that the private method

可以在上面的代码片段中看到private方法

def __diagnose_patient(self): # convention class name + method name

can be accessed as

可以作为访问

doctor._Doctor__diagnose_patient()

It’s just convention, although these can be accessed. It is a clear way of conveying the message to fellow developers. Don’t use it without having a clear understanding of what exactly it does.

尽管可以访问这些,但这只是约定。 这是向开发人员传达信息的一种清晰方法。 在没有清楚确切用途的情况下不要使用它

翻译自: https://towardsdatascience.com/private-members-in-python-217baf02a162

python3 私有成员

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值