“公共"或“私人" Python中的属性?什么是最好的方法? [英] "public" or "private" attribute in Python ? What is the best way?

查看:79
本文介绍了“公共"或“私人" Python中的属性?什么是最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我有以下示例类:

In Python, I have the following example class :

class Foo:
    self._attr = 0

    @property
    def attr(self):
        return self._attr

    @attr.setter
    def attr(self, value):
        self._attr = value

    @attr.deleter
    def attr(self):
        del self._attr

如您所见,我有一个简单的私有"属性"_attr"和一个用于访问它的属性.有很多代码可以声明一个简单的私有属性,我认为这样声明所有属性并不符合"KISS"哲学.

As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it's not respecting the "KISS" philosophy to declare all attributes like that.

那么,如果我不需要特定的getter/setter/deleter,为什么不将我的所有属性都声明为公共属性呢?

So, why not declare all my attributes as public attributes if I don't need a particular getter/setter/deleter ?

我的答案将是: 因为封装原理(OOP)另有说明!

My answer will be : Because the principle of encapsulation (OOP) says otherwise!

什么是最好的方法?

推荐答案

通常,Python代码会努力遵守统一访问原则.具体来说,可接受的方法是:

Typically, Python code strives to adhere to the Uniform Access Principle. Specifically, the accepted approach is:

  • 直接公开您的实例变量,例如允许使用foo.x = 0,而不允许使用foo.set_x(0)
  • 如果出于任何原因需要将访问包装在方法内部,请使用@property,它保留了访问语义.也就是说,foo.x = 0现在会调用foo.set_x(0).
  • Expose your instance variables directly, allowing, for instance, foo.x = 0, not foo.set_x(0)
  • If you need to wrap the accesses inside methods, for whatever reason, use @property, which preserves the access semantics. That is, foo.x = 0 now invokes foo.set_x(0).

此方法的主要优点是调用者可以执行此操作:

The main advantage to this approach is that the caller gets to do this:

foo.x += 1

即使代码可能确实在做:

even though the code might really be doing:

foo.set_x(foo.get_x() + 1)

第一个语句具有无限的可读性.但是,有了属性,您可以(在开始或之后)添加通过第二种方法获得的访问控制.

The first statement is infinitely more readable. Yet, with properties, you can add (at the beginning, or later on) the access control you get with the second approach.

也请注意,以单个下划线开头的实例变量通常是私有的.也就是说,下划线向其他开发人员发出信号,表示您认为该值是私有的,他们不应直接与之混淆.但是,使用语言 并不能阻止它们直接混乱.

Note, too, that instance variables starting with a single underscore are conventionally private. That is, the underscore signals to other developers that you consider the value to be private, and they shouldn't mess with it directly; however, nothing in the language prevents them from messing with it directly.

如果使用双引号下划线(例如__x),则Python会对名称进行一些混淆.但是,仍可以通过混淆的名称从类外部访问该变量.它不是真正的私人.只是有点...更不透明.还有反对使用双下划线的有效论点.一方面,它会使调试更加困难.

If you use a double leading underscore (e.g., __x), Python does a little obfuscation of the name. The variable is still accessible from outside the class, via its obfuscated name, however. It's not truly private. It's just kind of ... more opaque. And there are valid arguments against using the double underscore; for one thing, it can make debugging more difficult.

这篇关于“公共"或“私人" Python中的属性?什么是最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆