我是靠谱客的博主 大方月饼,最近开发中收集的这篇文章主要介绍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://radek.io/2011/07/21/private-protected-and-public-in-python/

    https://radek.io/2011/07/21/private-protected-and-public-in-python/

  • https://stackoverflow.com/questions/2064202/private-members-in-python

    https://stackoverflow.com/questions/2064202/private-members-in-python

  • https://mail.python.org/pipermail/tutor/2003-October/025932.html

    https://mail.python.org/pipermail/tutor/2003-October/025932.html

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

python3 私有成员

最后

以上就是大方月饼为你收集整理的python3 私有成员_Python中的私有成员的全部内容,希望文章能够帮你解决python3 私有成员_Python中的私有成员所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部