python 中的“交友”类

发布于 2024-11-15 04:49:54 字数 142 浏览 2 评论 0原文

有没有什么方法可以使类中的某些变量成为“私有”(或任何 self.__var 真正的变量),但可以被另一个类访问,就像 c++ 中的朋友一样,除了 python 之外?我不希望任何一个类中的变量被弄乱。我也不想复制整个代码并将其转换为第二个类。

Is there any way to make certain variables in classes "private" (or whatever self.__var really is) but be accessible to another class, like friends in c++, except in python? I do not want the variables in either class being messed with. Nor do I want to copy the entire code over and convert it for the second class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

于我来说 2024-11-22 04:49:54

不,没有这样的选择。

使用以单下划线开头的名称,并告诉参与您项目的其他人员不要对他们访问的内容感到愚蠢。

No, there is not such an option.

Use names that start with single underscores and tell the other people working on your project to not be silly about what they access.

狼性发作 2024-11-22 04:49:54

Python 的哲学是访问控制之类的问题取决于程序员的纪律。它不会尝试用语言编码程序的哪些部分是内部实现细节,哪些部分是文档化接口的一部分。因此,它不需要像 friend 这样的构造来尝试声明程序的哪些其他部分是类实现的一部分,哪些只是客户端。

这个想法是,如果你不能在不将这些概念部分编码到你的程序中的情况下编写/设计/记录/使用好的代码,那么当你对它们进行编码时你也可能无法做到这一点。因此,最好不要在语言中使用此类结构,因为它们不会增加语言的表达能力,有时还会造成妨碍。

The philosophy of Python is that issues like access control are up to programmer discipline. It doesn't attempt to encode in the language which parts of the program are internal implementation details, and which are part of the documented interface. Thus, it doesn't need constructs like friend to try to declare which other parts of the program are part of the implementation of a class and which are merely clients.

The idea is that if you can't write/design/document/use good code without partially encoding these concepts into your program, you probably can't do it when you are encoding them either. Therefore it's better not to have such constructs in the language, since they don't increase the expressive power of the language and occasionally they get in the way.

纸伞微斜 2024-11-22 04:49:54

python 中没有友元函数选项。
您可以选择使用单个下划线来定义受保护的变量,
但是在python中,受保护的变量也可以被主函数访问,但这并不完全是受保护变量的定义,看看吧。

class Student:
    _schoolName = 'XYZ School' # protected class attribute
    
    def __init__(self, name, age):
        self._name=name  # protected instance attribute
        self._age=age # protected instance attribute

std = Student("Swati", 25)
std._name
#answer is ->'Swati'
std._name = 'Dipa'
std._name
#answer is ->'Dipa'

there is no option of friend function in python.
you have an option to define a protected variable by using a single underscore,
but in python protected variable is also accessed by the main function, but this is not exactly the definition of a protected variable just have a look.

class Student:
    _schoolName = 'XYZ School' # protected class attribute
    
    def __init__(self, name, age):
        self._name=name  # protected instance attribute
        self._age=age # protected instance attribute

std = Student("Swati", 25)
std._name
#answer is ->'Swati'
std._name = 'Dipa'
std._name
#answer is ->'Dipa'
春风十里 2024-11-22 04:49:54

我不知道你在说什么。

>>> class Foo(object):
...   __bar = 42
... 
>>> class Quux(object):
...   def spam(self):
...     print Foo._Foo__bar
... 
>>> q = Quux()
>>> q.spam()
42

I have no clue what you're talking about.

>>> class Foo(object):
...   __bar = 42
... 
>>> class Quux(object):
...   def spam(self):
...     print Foo._Foo__bar
... 
>>> q = Quux()
>>> q.spam()
42
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文