标签归档:private

Python只读属性

问题:Python只读属性

我不知道何时属性应该是私有的,是否应该使用属性。

我最近读到,setter和getters不是pythonic,我应该使用属性装饰器。没关系。

但是,如果我有属性,该属性不能从类外部设置,而是可以读取的(只读属性)。这个属性应该是私有的self._x吗?我所说的私有是指下划线吗?如果是,那么不使用getter怎么读?我现在知道的唯一方法是写

@property
def x(self):
    return self._x

这样我就可以读取属性,obj.x但是我无法设置它,obj.x = 1所以很好。

但是,我真的应该在乎设置不应该设置的对象吗?也许我应该离开它。但是话又说回来,我不能使用下划线,因为阅读obj._x对于用户来说很奇怪,所以我应该使用下划线obj.x,然后用户又一次不知道他一定不能设置该属性。

您的看法和做法是什么?

I don’t know when attribute should be private and if I should use property.

I read recently that setters and getters are not pythonic and I should use property decorator. It’s ok.

But what if I have attribute, that mustn’t be set from outside of class but can be read (read-only attribute). Should this attribute be private, and by private I mean with underscore, like that self._x? If yes then how can I read it without using getter? Only method I know right now is to write

@property
def x(self):
    return self._x

That way I can read attribute by obj.x but I can’t set it obj.x = 1 so it’s fine.

But should I really care about setting object that mustn’t be set? Maybe I should just leave it. But then again I can’t use underscore because reading obj._x is odd for user, so I should use obj.x and then again user doesn’t know that he mustn’t set this attribute.

What’s your opinion and practics?


回答 0

通常,在编写Python程序时应假定所有用户都同意成年人,因此他们有责任自己正确使用事物。但是,在极少数情况下,无法设置属性(例如派生值或从某个静态数据源读取的值)就没有意义,仅使用吸气剂的属性通常是首选模式。

Generally, Python programs should be written with the assumption that all users are consenting adults, and thus are responsible for using things correctly themselves. However, in the rare instance where it just does not make sense for an attribute to be settable (such as a derived value, or a value read from some static datasource), the getter-only property is generally the preferred pattern.


回答 1

西拉斯·雷Silas Ray)只是我的两分钱,走在正确的轨道上,但是我觉得自己想举个例子。;-)

Python是一种类型不安全的语言,因此,您始终必须信任代码的用户才能像合理的(明智的)人员一样使用代码。

根据PEP 8

仅对非公共方法和实例变量使用前导下划线。

要在类中具有“只读”属性,您可以使用@property修饰,您需要在继承object时使用新样式的类来进行继承。

例:

>>> class A(object):
...     def __init__(self, a):
...         self._a = a
...
...     @property
...     def a(self):
...         return self._a
... 
>>> a = A('test')
>>> a.a
'test'
>>> a.a = 'pleh'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

Just my two cents, Silas Ray is on the right track, however I felt like adding an example. ;-)

Python is a type-unsafe language and thus you’ll always have to trust the users of your code to use the code like a reasonable (sensible) person.

Per PEP 8:

Use one leading underscore only for non-public methods and instance variables.

To have a ‘read-only’ property in a class you can make use of the @property decoration, you’ll need to inherit from object when you do so to make use of the new-style classes.

Example:

>>> class A(object):
...     def __init__(self, a):
...         self._a = a
...
...     @property
...     def a(self):
...         return self._a
... 
>>> a = A('test')
>>> a.a
'test'
>>> a.a = 'pleh'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

回答 2

这是一种避免假设的方法

所有使用者都是成年人,因此有责任自行正确使用事物。

请在下面查看我的更新

使用@property,非常冗长,例如:

   class AClassWithManyAttributes:
        '''refactored to properties'''
        def __init__(a, b, c, d, e ...)
             self._a = a
             self._b = b
             self._c = c
             self.d = d
             self.e = e

        @property
        def a(self):
            return self._a
        @property
        def b(self):
            return self._b
        @property
        def c(self):
            return self._c
        # you get this ... it's long

使用

没有下划线:这是一个公共变量。
一个下划线:这是一个受保护的变量。
有两个下划线:这是一个私有变量。

除了最后一个,这是一个约定。如果您确实努力尝试,仍然可以使用双下划线访问变量。

那么我们该怎么办?我们是否放弃使用Python中的只读属性?

看哪!read_only_properties装潢抢救!

@read_only_properties('readonly', 'forbidden')
class MyClass(object):
    def __init__(self, a, b, c):
        self.readonly = a
        self.forbidden = b
        self.ok = c

m = MyClass(1, 2, 3)
m.ok = 4
# we can re-assign a value to m.ok
# read only access to m.readonly is OK 
print(m.ok, m.readonly) 
print("This worked...")
# this will explode, and raise AttributeError
m.forbidden = 4

你问:

哪里read_only_properties来的?

很高兴您询问,这是read_only_properties的来源:

def read_only_properties(*attrs):

    def class_rebuilder(cls):
        "The class decorator"

        class NewClass(cls):
            "This is the overwritten class"
            def __setattr__(self, name, value):
                if name not in attrs:
                    pass
                elif name not in self.__dict__:
                    pass
                else:
                    raise AttributeError("Can't modify {}".format(name))

                super().__setattr__(name, value)
        return NewClass
    return class_rebuilder

更新

我没想到这个答案会引起如此多的关注。令人惊讶的是。这鼓励我创建一个可以使用的软件包。

$ pip install read-only-properties

在您的python shell中:

In [1]: from rop import read_only_properties

In [2]: @read_only_properties('a')
   ...: class Foo:
   ...:     def __init__(self, a, b):
   ...:         self.a = a
   ...:         self.b = b
   ...:         

In [3]: f=Foo('explodes', 'ok-to-overwrite')

In [4]: f.b = 5

In [5]: f.a = 'boom'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-a5226072b3b4> in <module>()
----> 1 f.a = 'boom'

/home/oznt/.virtualenvs/tracker/lib/python3.5/site-packages/rop.py in __setattr__(self, name, value)
    116                     pass
    117                 else:
--> 118                     raise AttributeError("Can't touch {}".format(name))
    119 
    120                 super().__setattr__(name, value)

AttributeError: Can't touch a

Here is a way to avoid the assumption that

all users are consenting adults, and thus are responsible for using things correctly themselves.

please see my update below

Using @property, is very verbose e.g.:

   class AClassWithManyAttributes:
        '''refactored to properties'''
        def __init__(a, b, c, d, e ...)
             self._a = a
             self._b = b
             self._c = c
             self.d = d
             self.e = e

        @property
        def a(self):
            return self._a
        @property
        def b(self):
            return self._b
        @property
        def c(self):
            return self._c
        # you get this ... it's long

Using

No underscore: it’s a public variable.
One underscore: it’s a protected variable.
Two underscores: it’s a private variable.

Except the last one, it’s a convention. You can still, if you really try hard, access variables with double underscore.

So what do we do? Do we give up on having read only properties in Python?

Behold! read_only_properties decorator to the rescue!

@read_only_properties('readonly', 'forbidden')
class MyClass(object):
    def __init__(self, a, b, c):
        self.readonly = a
        self.forbidden = b
        self.ok = c

m = MyClass(1, 2, 3)
m.ok = 4
# we can re-assign a value to m.ok
# read only access to m.readonly is OK 
print(m.ok, m.readonly) 
print("This worked...")
# this will explode, and raise AttributeError
m.forbidden = 4

You ask:

Where is read_only_properties coming from?

Glad you asked, here is the source for read_only_properties:

def read_only_properties(*attrs):

    def class_rebuilder(cls):
        "The class decorator"

        class NewClass(cls):
            "This is the overwritten class"
            def __setattr__(self, name, value):
                if name not in attrs:
                    pass
                elif name not in self.__dict__:
                    pass
                else:
                    raise AttributeError("Can't modify {}".format(name))

                super().__setattr__(name, value)
        return NewClass
    return class_rebuilder

update

I never expected this answer will get so much attention. Surprisingly it does. This encouraged me to create a package you can use.

$ pip install read-only-properties

in your python shell:

In [1]: from rop import read_only_properties

In [2]: @read_only_properties('a')
   ...: class Foo:
   ...:     def __init__(self, a, b):
   ...:         self.a = a
   ...:         self.b = b
   ...:         

In [3]: f=Foo('explodes', 'ok-to-overwrite')

In [4]: f.b = 5

In [5]: f.a = 'boom'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-a5226072b3b4> in <module>()
----> 1 f.a = 'boom'

/home/oznt/.virtualenvs/tracker/lib/python3.5/site-packages/rop.py in __setattr__(self, name, value)
    116                     pass
    117                 else:
--> 118                     raise AttributeError("Can't touch {}".format(name))
    119 
    120                 super().__setattr__(name, value)

AttributeError: Can't touch a

回答 3

这是一种对只读属性略有不同的方法,由于必须对它们进行初始化,因此应该将它们称为一次写入属性,不是吗?对于那些担心直接通过访问对象字典来修改属性的偏执狂,我引入了“极端”名称处理:

from uuid import uuid4

class Read_Only_Property:
    def __init__(self, name):
        self.name = name
        self.dict_name = uuid4().hex
        self.initialized = False

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return instance.__dict__[self.dict_name]

    def __set__(self, instance, value):
        if self.initialized:
            raise AttributeError("Attempt to modify read-only property '%s'." % self.name)
        instance.__dict__[self.dict_name] = value
        self.initialized = True

class Point:
    x = Read_Only_Property('x')
    y = Read_Only_Property('y')
    def __init__(self, x, y):
        self.x = x
        self.y = y

if __name__ == '__main__':
    try:
        p = Point(2, 3)
        print(p.x, p.y)
        p.x = 9
    except Exception as e:
        print(e)

Here is a slightly different approach to read-only properties, which perhaps should be called write-once properties since they do have to get initialized, don’t they? For the paranoid among us who worry about being able to modify properties by accessing the object’s dictionary directly, I’ve introduced “extreme” name mangling:

from uuid import uuid4

class Read_Only_Property:
    def __init__(self, name):
        self.name = name
        self.dict_name = uuid4().hex
        self.initialized = False

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return instance.__dict__[self.dict_name]

    def __set__(self, instance, value):
        if self.initialized:
            raise AttributeError("Attempt to modify read-only property '%s'." % self.name)
        instance.__dict__[self.dict_name] = value
        self.initialized = True

class Point:
    x = Read_Only_Property('x')
    y = Read_Only_Property('y')
    def __init__(self, x, y):
        self.x = x
        self.y = y

if __name__ == '__main__':
    try:
        p = Point(2, 3)
        print(p.x, p.y)
        p.x = 9
    except Exception as e:
        print(e)

回答 4

我对创建只读属性的前两个答案不满意,因为第一个解决方案允许删除readonly属性,然后进行设置,并且不会阻止__dict__。第二种解决方案可以与测试一起解决-找到等于您将其设置为2的值并最终进行更改。

现在,获取代码。

def final(cls):
    clss = cls
    @classmethod
    def __init_subclass__(cls, **kwargs):
        raise TypeError("type '{}' is not an acceptable base type".format(clss.__name__))
    cls.__init_subclass__ = __init_subclass__
    return cls


def methoddefiner(cls, method_name):
    for clss in cls.mro():
        try:
            getattr(clss, method_name)
            return clss
        except(AttributeError):
            pass
    return None


def readonlyattributes(*attrs):
    """Method to create readonly attributes in a class

    Use as a decorator for a class. This function takes in unlimited 
    string arguments for names of readonly attributes and returns a
    function to make the readonly attributes readonly. 

    The original class's __getattribute__, __setattr__, and __delattr__ methods
    are redefined so avoid defining those methods in the decorated class

    You may create setters and deleters for readonly attributes, however
    if they are overwritten by the subclass, they lose access to the readonly
    attributes. 

    Any method which sets or deletes a readonly attribute within
    the class loses access if overwritten by the subclass besides the __new__
    or __init__ constructors.

    This decorator doesn't support subclassing of these classes
    """
    def classrebuilder(cls):
        def __getattribute__(self, name):
            if name == '__dict__':
                    from types import MappingProxyType
                    return MappingProxyType(super(cls, self).__getattribute__('__dict__'))
            return super(cls, self).__getattribute__(name)
        def __setattr__(self, name, value): 
                if name == '__dict__' or name in attrs:
                    import inspect
                    stack = inspect.stack()
                    try:
                        the_class = stack[1][0].f_locals['self'].__class__
                    except(KeyError):
                        the_class = None
                    the_method = stack[1][0].f_code.co_name
                    if the_class != cls: 
                         if methoddefiner(type(self), the_method) != cls:
                            raise AttributeError("Cannot set readonly attribute '{}'".format(name))                        
                return super(cls, self).__setattr__(name, value)
        def __delattr__(self, name):                
                if name == '__dict__' or name in attrs:
                    import inspect
                    stack = inspect.stack()
                    try:
                        the_class = stack[1][0].f_locals['self'].__class__
                    except(KeyError):
                        the_class = None
                    the_method = stack[1][0].f_code.co_name
                    if the_class != cls:
                        if methoddefiner(type(self), the_method) != cls:
                            raise AttributeError("Cannot delete readonly attribute '{}'".format(name))                        
                return super(cls, self).__delattr__(name)
        clss = cls
        cls.__getattribute__ = __getattribute__
        cls.__setattr__ = __setattr__
        cls.__delattr__ = __delattr__
        #This line will be moved when this algorithm will be compatible with inheritance
        cls = final(cls)
        return cls
    return classrebuilder

def setreadonlyattributes(cls, *readonlyattrs):
    return readonlyattributes(*readonlyattrs)(cls)


if __name__ == '__main__':
    #test readonlyattributes only as an indpendent module
    @readonlyattributes('readonlyfield')
    class ReadonlyFieldClass(object):
        def __init__(self, a, b):
            #Prevent initalization of the internal, unmodified PrivateFieldClass
            #External PrivateFieldClass can be initalized
            self.readonlyfield = a
            self.publicfield = b


    attr = None
    def main():
        global attr
        pfi = ReadonlyFieldClass('forbidden', 'changable')
        ###---test publicfield, ensure its mutable---###
        try:
            #get publicfield
            print(pfi.publicfield)
            print('__getattribute__ works')
            #set publicfield
            pfi.publicfield = 'mutable'
            print('__setattr__ seems to work')
            #get previously set publicfield
            print(pfi.publicfield)
            print('__setattr__ definitely works')
            #delete publicfield
            del pfi.publicfield 
            print('__delattr__ seems to work')
            #get publicfield which was supposed to be deleted therefore should raise AttributeError
            print(pfi.publlicfield)
            #publicfield wasn't deleted, raise RuntimeError
            raise RuntimeError('__delattr__ doesn\'t work')
        except(AttributeError):
            print('__delattr__ works')


        try:
            ###---test readonly, make sure its readonly---###
            #get readonlyfield
            print(pfi.readonlyfield)
            print('__getattribute__ works')
            #set readonlyfield, should raise AttributeError
            pfi.readonlyfield = 'readonly'
            #apparently readonlyfield was set, notify user
            raise RuntimeError('__setattr__ doesn\'t work')
        except(AttributeError):
            print('__setattr__ seems to work')
            try:
                #ensure readonlyfield wasn't set
                print(pfi.readonlyfield)
                print('__setattr__ works')
                #delete readonlyfield
                del pfi.readonlyfield
                #readonlyfield was deleted, raise RuntimeError
                raise RuntimeError('__delattr__ doesn\'t work')
            except(AttributeError):
                print('__delattr__ works')
        try:
            print("Dict testing")
            print(pfi.__dict__, type(pfi.__dict__))
            attr = pfi.readonlyfield
            print(attr)
            print("__getattribute__ works")
            if pfi.readonlyfield != 'forbidden':
                print(pfi.readonlyfield)
                raise RuntimeError("__getattr__ doesn't work")
            try:
                pfi.__dict__ = {}
                raise RuntimeError("__setattr__ doesn't work")
            except(AttributeError):
                print("__setattr__ works")
            del pfi.__dict__
            raise RuntimeError("__delattr__ doesn't work")
        except(AttributeError):
            print(pfi.__dict__)
            print("__delattr__ works")
            print("Basic things work")


main()

除非您编写库代码时使用只读属性,否则将这些属性设置为只读代码是为了增强他们的程序而将代码分发给其他人使用,而不是用于其他目的(例如应用程序开发)的代码。解决了__dict__问题,因为__dict__现在是不可变的类型。MappingProxyType,因此无法通过__dict__更改属性。设置或删除__dict__也被阻止。更改只读属性的唯一方法是更改​​类本身的方法。

尽管我认为我的解决方案比前两个解决方案要好,但可以改进。这些是此代码的弱点:

a)不允许在子类中添加设置或删除只读属性的方法。即使调用了超类的方法,也会自动禁止子类中定义的方法访问只读属性。

b)可以更改类的只读方法以克服只读限制。

但是,没有办法不编辑类来设置或删除只读属性。这不依赖于命名约定,这很好,因为Python与命名约定不太一致。这提供了一种方法,使只读属性无法通过隐藏的漏洞进行更改,而无需编辑类本身。只需在将装饰器作为参数调用时列出要只读的属性即可,它们将变为只读。

归功于Brice的回答:如何在python中另一个类的函数中获取调用方类名称?获取调用方的类和方法。

I am dissatisfied with the previous two answers to create read only properties because the first solution allows the readonly attribute to be deleted and then set and doesn’t block the __dict__. The second solution could be worked around with testing – finding the value that equals what you set it two and changing it eventually.

Now, for the code.

def final(cls):
    clss = cls
    @classmethod
    def __init_subclass__(cls, **kwargs):
        raise TypeError("type '{}' is not an acceptable base type".format(clss.__name__))
    cls.__init_subclass__ = __init_subclass__
    return cls


def methoddefiner(cls, method_name):
    for clss in cls.mro():
        try:
            getattr(clss, method_name)
            return clss
        except(AttributeError):
            pass
    return None


def readonlyattributes(*attrs):
    """Method to create readonly attributes in a class

    Use as a decorator for a class. This function takes in unlimited 
    string arguments for names of readonly attributes and returns a
    function to make the readonly attributes readonly. 

    The original class's __getattribute__, __setattr__, and __delattr__ methods
    are redefined so avoid defining those methods in the decorated class

    You may create setters and deleters for readonly attributes, however
    if they are overwritten by the subclass, they lose access to the readonly
    attributes. 

    Any method which sets or deletes a readonly attribute within
    the class loses access if overwritten by the subclass besides the __new__
    or __init__ constructors.

    This decorator doesn't support subclassing of these classes
    """
    def classrebuilder(cls):
        def __getattribute__(self, name):
            if name == '__dict__':
                    from types import MappingProxyType
                    return MappingProxyType(super(cls, self).__getattribute__('__dict__'))
            return super(cls, self).__getattribute__(name)
        def __setattr__(self, name, value): 
                if name == '__dict__' or name in attrs:
                    import inspect
                    stack = inspect.stack()
                    try:
                        the_class = stack[1][0].f_locals['self'].__class__
                    except(KeyError):
                        the_class = None
                    the_method = stack[1][0].f_code.co_name
                    if the_class != cls: 
                         if methoddefiner(type(self), the_method) != cls:
                            raise AttributeError("Cannot set readonly attribute '{}'".format(name))                        
                return super(cls, self).__setattr__(name, value)
        def __delattr__(self, name):                
                if name == '__dict__' or name in attrs:
                    import inspect
                    stack = inspect.stack()
                    try:
                        the_class = stack[1][0].f_locals['self'].__class__
                    except(KeyError):
                        the_class = None
                    the_method = stack[1][0].f_code.co_name
                    if the_class != cls:
                        if methoddefiner(type(self), the_method) != cls:
                            raise AttributeError("Cannot delete readonly attribute '{}'".format(name))                        
                return super(cls, self).__delattr__(name)
        clss = cls
        cls.__getattribute__ = __getattribute__
        cls.__setattr__ = __setattr__
        cls.__delattr__ = __delattr__
        #This line will be moved when this algorithm will be compatible with inheritance
        cls = final(cls)
        return cls
    return classrebuilder

def setreadonlyattributes(cls, *readonlyattrs):
    return readonlyattributes(*readonlyattrs)(cls)


if __name__ == '__main__':
    #test readonlyattributes only as an indpendent module
    @readonlyattributes('readonlyfield')
    class ReadonlyFieldClass(object):
        def __init__(self, a, b):
            #Prevent initalization of the internal, unmodified PrivateFieldClass
            #External PrivateFieldClass can be initalized
            self.readonlyfield = a
            self.publicfield = b


    attr = None
    def main():
        global attr
        pfi = ReadonlyFieldClass('forbidden', 'changable')
        ###---test publicfield, ensure its mutable---###
        try:
            #get publicfield
            print(pfi.publicfield)
            print('__getattribute__ works')
            #set publicfield
            pfi.publicfield = 'mutable'
            print('__setattr__ seems to work')
            #get previously set publicfield
            print(pfi.publicfield)
            print('__setattr__ definitely works')
            #delete publicfield
            del pfi.publicfield 
            print('__delattr__ seems to work')
            #get publicfield which was supposed to be deleted therefore should raise AttributeError
            print(pfi.publlicfield)
            #publicfield wasn't deleted, raise RuntimeError
            raise RuntimeError('__delattr__ doesn\'t work')
        except(AttributeError):
            print('__delattr__ works')


        try:
            ###---test readonly, make sure its readonly---###
            #get readonlyfield
            print(pfi.readonlyfield)
            print('__getattribute__ works')
            #set readonlyfield, should raise AttributeError
            pfi.readonlyfield = 'readonly'
            #apparently readonlyfield was set, notify user
            raise RuntimeError('__setattr__ doesn\'t work')
        except(AttributeError):
            print('__setattr__ seems to work')
            try:
                #ensure readonlyfield wasn't set
                print(pfi.readonlyfield)
                print('__setattr__ works')
                #delete readonlyfield
                del pfi.readonlyfield
                #readonlyfield was deleted, raise RuntimeError
                raise RuntimeError('__delattr__ doesn\'t work')
            except(AttributeError):
                print('__delattr__ works')
        try:
            print("Dict testing")
            print(pfi.__dict__, type(pfi.__dict__))
            attr = pfi.readonlyfield
            print(attr)
            print("__getattribute__ works")
            if pfi.readonlyfield != 'forbidden':
                print(pfi.readonlyfield)
                raise RuntimeError("__getattr__ doesn't work")
            try:
                pfi.__dict__ = {}
                raise RuntimeError("__setattr__ doesn't work")
            except(AttributeError):
                print("__setattr__ works")
            del pfi.__dict__
            raise RuntimeError("__delattr__ doesn't work")
        except(AttributeError):
            print(pfi.__dict__)
            print("__delattr__ works")
            print("Basic things work")


main()

There is no point to making read only attributes except when your writing library code, code which is being distributed to others as code to use in order to enhance their programs, not code for any other purpose, like app development. The __dict__ problem is solved, because the __dict__ is now of the immutable types.MappingProxyType, so attributes cannot be changed through the __dict__. Setting or deleting __dict__ is also blocked. The only way to change read only properties is through changing the methods of the class itself.

Though I believe my solution is better than of the previous two, it could be improved. These are this code’s weaknesses:

a) Doesn’t allow adding to a method in a subclass which sets or deletes a readonly attribute. A method defined in a subclass is automatically barred from accessing a readonly attribute, even by calling the superclass’ version of the method.

b) The class’ readonly methods can be changed to defeat the read only restrictions.

However, there is not way without editing the class to set or delete a read only attribute. This isn’t dependent on naming conventions, which is good because Python isn’t so consistent with naming conventions. This provides a way to make read only attributes that cannot be changed with hidden loopholes without editing the class itself. Simply list the attributes to be read only when calling the decorator as arguments and they will become read only.

Credit to Brice’s answer in How to get the caller class name inside a function of another class in python? for getting the caller classes and methods.


回答 5

注意,实例方法也是(类的)属性,如果您确实想成为坏蛋,则可以在类或实例级别设置它们。或者,您可以设置一个类变量(这也是该类的一个属性),在该变量中,方便的只读属性将无法立即使用。我要说的是,“只读属性”问题实际上比通常认为的要普遍得多。幸运的是,人们对工作的传统期望是如此强烈,以至于使我们在其他情况下视而不见(毕竟,几乎所有东西都是python中的某种属性)。

基于这些期望,我认为最通用,最轻便的方法是采用以下约定:“公开”(无前导下划线)属性是只读的,除非明确记录为可写。这包含了通常的期望,即不会对方法进行修补,而指示实例默认值的类变量则更不用说了。如果您真的对某些特殊属性感到偏执,请使用只读描述符作为最后的资源度量。

Notice that instance methods are also attributes (of the class) and that you could set them at the class or instance level if you really wanted to be a badass. Or that you may set a class variable (which is also an attribute of the class), where handy readonly properties won’t work neatly out of the box. What I’m trying to say is that the “readonly attribute” problem is in fact more general than it’s usually perceived to be. Fortunately there are conventional expectations at work that are so strong as to blind us wrt these other cases (after all, almost everything is an attribute of some sort in python).

Building upon these expectations I think the most general and lightweight approach is to adopt the convention that “public” (no leading underscore) attributes are readonly except when explicitly documented as writeable. This subsumes the usual expectation that methods won’t be patched and class variables indicating instance defaults are better let alone. If you feel really paranoid about some special attribute, use a readonly descriptor as a last resource measure.


回答 6

尽管我喜欢Oz123的类装饰器,但是您也可以执行以下操作,该操作使用显式类包装器和__new__以及类Factory方法,以在闭包内返回类:

class B(object):
    def __new__(cls, val):
        return cls.factory(val)

@classmethod
def factory(cls, val):
    private = {'var': 'test'}

    class InnerB(object):
        def __init__(self):
            self.variable = val
            pass

        @property
        def var(self):
            return private['var']

    return InnerB()

While I like the class decorator from Oz123, you could also do the following, which uses an explicit class wrapper and __new__ with a class Factory method returning the class within a closure:

class B(object):
    def __new__(cls, val):
        return cls.factory(val)

@classmethod
def factory(cls, val):
    private = {'var': 'test'}

    class InnerB(object):
        def __init__(self):
            self.variable = val
            pass

        @property
        def var(self):
            return private['var']

    return InnerB()

回答 7

那是我的解决方法。

@property
def language(self):
    return self._language
@language.setter
def language(self, value):
    # WORKAROUND to get a "getter-only" behavior
    # set the value only if the attribute does not exist
    try:
        if self.language == value:
            pass
        print("WARNING: Cannot set attribute \'language\'.")
    except AttributeError:
        self._language = value

That’s my workaround.

@property
def language(self):
    return self._language
@language.setter
def language(self, value):
    # WORKAROUND to get a "getter-only" behavior
    # set the value only if the attribute does not exist
    try:
        if self.language == value:
            pass
        print("WARNING: Cannot set attribute \'language\'.")
    except AttributeError:
        self._language = value

回答 8

有人提到使用代理对象,但我没有看到这样的示例,所以我最终尝试了一下,[可怜]。

/!\如果可能,请更喜欢类定义和类构造函数

这段代码可以有效地重写class.__new__(类构造函数),但在各个方面都更糟。减轻痛苦,如果可以,请不要使用此模式。

def attr_proxy(obj):
    """ Use dynamic class definition to bind obj and proxy_attrs.
        If you can extend the target class constructor that is 
        cleaner, but its not always trivial to do so.
    """
    proxy_attrs = dict()

    class MyObjAttrProxy():
        def __getattr__(self, name):
            if name in proxy_attrs:
                return proxy_attrs[name]  # overloaded

            return getattr(obj, name)  # proxy

        def __setattr__(self, name, value):
            """ note, self is not bound when overloading methods
            """
            proxy_attrs[name] = value

    return MyObjAttrProxy()


myobj = attr_proxy(Object())
setattr(myobj, 'foo_str', 'foo')

def func_bind_obj_as_self(func, self):
    def _method(*args, **kwargs):
        return func(self, *args, **kwargs)
    return _method

def mymethod(self, foo_ct):
    """ self is not bound because we aren't using object __new__
        you can write the __setattr__ method to bind a self 
        argument, or declare your functions dynamically to bind in 
        a static object reference.
    """
    return self.foo_str + foo_ct

setattr(myobj, 'foo', func_bind_obj_as_self(mymethod, myobj))

someone mentioned using a proxy object, I didn’t see an example of that so I ended up trying it out, [poorly].

/!\ Please prefer class definitions and class constructors if possible

this code is effectively re-writing class.__new__ (class constructor) except worse in every way. Save yourself the pain and do not use this pattern if you can.

def attr_proxy(obj):
    """ Use dynamic class definition to bind obj and proxy_attrs.
        If you can extend the target class constructor that is 
        cleaner, but its not always trivial to do so.
    """
    proxy_attrs = dict()

    class MyObjAttrProxy():
        def __getattr__(self, name):
            if name in proxy_attrs:
                return proxy_attrs[name]  # overloaded

            return getattr(obj, name)  # proxy

        def __setattr__(self, name, value):
            """ note, self is not bound when overloading methods
            """
            proxy_attrs[name] = value

    return MyObjAttrProxy()


myobj = attr_proxy(Object())
setattr(myobj, 'foo_str', 'foo')

def func_bind_obj_as_self(func, self):
    def _method(*args, **kwargs):
        return func(self, *args, **kwargs)
    return _method

def mymethod(self, foo_ct):
    """ self is not bound because we aren't using object __new__
        you can write the __setattr__ method to bind a self 
        argument, or declare your functions dynamically to bind in 
        a static object reference.
    """
    return self.foo_str + foo_ct

setattr(myobj, 'foo', func_bind_obj_as_self(mymethod, myobj))

回答 9

我知道我从头开始带回了这个线程,但是我正在研究如何使属性变为只读,并且在找到该主题之后,我对已经共享的解决方案不满意。

因此,如果您从以下代码开始,请回到最初的问题:

@property
def x(self):
    return self._x

并且您想将X设为只读,只需添加:

@x.setter
def x(self, value):
    raise Exception("Member readonly")

然后,如果您运行以下命令:

print (x) # Will print whatever X value is
x = 3 # Will raise exception "Member readonly"

I know i’m bringing back from the dead this thread, but I was looking at how to make a property read only and after finding this topic, I wasn’t satisfied with the solutions already shared.

So, going back to the initial question, if you start with this code:

@property
def x(self):
    return self._x

And you want to make X readonly, you can just add:

@x.setter
def x(self, value):
    raise Exception("Member readonly")

Then, if you run the following:

print (x) # Will print whatever X value is
x = 3 # Will raise exception "Member readonly"

在python中定义私有模块函数

问题:在python中定义私有模块函数

根据http://www.faqs.org/docs/diveintopython/fileinfo_private.html

像大多数语言一样,Python具有私有元素的概念:

  • 私有函数,不能从其模块外部调用

但是,如果我定义两个文件:

#a.py
__num=1

和:

#b.py
import a
print a.__num

当我运行b.py它打印出1没有给出任何exceptions。diveintopython是错误的,还是我误会了某些东西?而且是有一些方法可以定义模块的功能为私有?

According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html:

Like most languages, Python has the concept of private elements:

  • Private functions, which can’t be called from outside their module

However, if I define two files:

#a.py
__num=1

and:

#b.py
import a
print a.__num

when i run b.py it prints out 1 without giving any exception. Is diveintopython wrong, or did I misunderstand something? And is there some way to do define a module’s function as private?


回答 0

在Python中,“隐私”取决于“同意成年人”的协议级别-您不能强制执行它(比现实生活中的要多;-)。单个前导下划线表示您不应该 “从外部”访问它- 两个前导下划线(不带尾随下划线)可以更加有力地传达信息……但最终,它仍然取决于社交网络会议达成共识:Python的自省是有力的,以至于你无法手铐在世界上其他程序员尊重您的意愿。

((顺便说一句,尽管这是一个秘密的秘密,但对于C ++却是如此:在大多数编译器中,狡猾的编码人员只需花#define private public一行简单#include.h代码就可以对您的“隐私”进行散列…!-) )

In Python, “privacy” depends on “consenting adults'” levels of agreement – you can’t force it (any more than you can in real life;-). A single leading underscore means you’re not supposed to access it “from the outside” — two leading underscores (w/o trailing underscores) carry the message even more forcefully… but, in the end, it still depends on social convention and consensus: Python’s introspection is forceful enough that you can’t handcuff every other programmer in the world to respect your wishes.

((Btw, though it’s a closely held secret, much the same holds for C++: with most compilers, a simple #define private public line before #includeing your .h file is all it takes for wily coders to make hash of your “privacy”…!-))


回答 1

类私有模块私有之间可能会有混淆。

模块私人与启动一个下划线
这样的元件不使用时沿复制from <module_name> import *导入命令的形式; 但是,如果使用import <moudule_name>语法将其导入(请参阅Ben Wilhelm的答案),
只需从问题示例的a .__ num中删除一个下划线,并且不会在使用该from a import *语法导入a.py的模块中显示该下划线。

类私有与开始两个下划线 (又名dunder即d-ouble下分数)
这样的变量有其名“错位”,以包括类名等
它仍然可以访问的类逻辑的外面,通过重整名称。
尽管名称改编可以用作防止未经授权访问的温和预防工具,但其主要目的是防止与祖先类的类成员发生可能的名称冲突。参见亚历克斯·马特利(Alex Martelli)有趣而准确地提及成年人的同意书,因为他描述了有关这些变量的约定。

>>> class Foo(object):
...    __bar = 99
...    def PrintBar(self):
...        print(self.__bar)
...
>>> myFoo = Foo()
>>> myFoo.__bar  #direct attempt no go
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__bar'
>>> myFoo.PrintBar()  # the class itself of course can access it
99
>>> dir(Foo)    # yet can see it
['PrintBar', '_Foo__bar', '__class__', '__delattr__', '__dict__', '__doc__', '__
format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__
', '__subclasshook__', '__weakref__']
>>> myFoo._Foo__bar  #and get to it by its mangled name !  (but I shouldn't!!!)
99
>>>

There may be confusion between class privates and module privates.

A module private starts with one underscore
Such a element is not copied along when using the from <module_name> import * form of the import command; it is however imported if using the import <moudule_name> syntax (see Ben Wilhelm’s answer)
Simply remove one underscore from the a.__num of the question’s example and it won’t show in modules that import a.py using the from a import * syntax.

A class private starts with two underscores (aka dunder i.e. d-ouble under-score)
Such a variable has its name “mangled” to include the classname etc.
It can still be accessed outside of the class logic, through the mangled name.
Although the name mangling can serve as a mild prevention device against unauthorized access, its main purpose is to prevent possible name collisions with class members of the ancestor classes. See Alex Martelli’s funny but accurate reference to consenting adults as he describes the convention used in regards to these variables.

>>> class Foo(object):
...    __bar = 99
...    def PrintBar(self):
...        print(self.__bar)
...
>>> myFoo = Foo()
>>> myFoo.__bar  #direct attempt no go
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__bar'
>>> myFoo.PrintBar()  # the class itself of course can access it
99
>>> dir(Foo)    # yet can see it
['PrintBar', '_Foo__bar', '__class__', '__delattr__', '__dict__', '__doc__', '__
format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__
', '__subclasshook__', '__weakref__']
>>> myFoo._Foo__bar  #and get to it by its mangled name !  (but I shouldn't!!!)
99
>>>

回答 2

由于模块隐私不是纯粹的常规,并且由于使用import可能会或可能不会识别模块隐私,这取决于使用方式,因此未完全回答此问题。

如果您在模块中定义专用名称,则这些名称被导入到使用“ import module_name”语法的任何脚本中。因此,假设您已在示例中正确定义了a.py中的私有_num模块,如下所示。

#a.py
_num=1

..您将可以在b.py中使用模块名称符号访问它:

#b.py
import a
...
foo = a._num # 1

要仅从a.py导入非特权,必须使用from语法:

#b.py
from a import *
...
foo = _num # throws NameError: name '_num' is not defined

但是,为了清楚起见,从模块导入名称时最好是显式的,而不是用’*’导入所有名称:

#b.py
from a import name1 
from a import name2
...

This question was not fully answered, since module privacy is not purely conventional, and since using import may or may not recognize module privacy, depending on how it is used.

If you define private names in a module, those names will be imported into any script that uses the syntax, ‘import module_name’. Thus, assuming you had correctly defined in your example the module private, _num, in a.py, like so..

#a.py
_num=1

..you would be able to access it in b.py with the module name symbol:

#b.py
import a
...
foo = a._num # 1

To import only non-privates from a.py, you must use the from syntax:

#b.py
from a import *
...
foo = _num # throws NameError: name '_num' is not defined

For the sake of clarity, however, it is better to be explicit when importing names from modules, rather than importing them all with a ‘*’:

#b.py
from a import name1 
from a import name2
...

回答 3

Python允许带有双下划线前缀的私有成员。该技术在模块级别上不起作用,因此我认为这是Dive Into Python中的错误。

这是私有类函数的示例:

class foo():
    def bar(self): pass
    def __bar(self): pass

f = foo()
f.bar()   # this call succeeds
f.__bar() # this call fails

Python allows for private class members with the double underscore prefix. This technique doesn’t work at a module level so I am thinking this is a mistake in Dive Into Python.

Here is an example of private class functions:

class foo():
    def bar(self): pass
    def __bar(self): pass

f = foo()
f.bar()   # this call succeeds
f.__bar() # this call fails

回答 4

您可以添加一个内部函数:

def public(self, args):
   def private(self.root, data):
       if (self.root != None):
          pass #do something with data

如果您确实需要该级别的隐私,则应采用类似的方法。

You can add an inner function:

def public(self, args):
   def private(self.root, data):
       if (self.root != None):
          pass #do something with data

Something like that if you really need that level of privacy.


回答 5

这是一个古老的问题,但是标准文档现在涵盖了模块私有(一个下划线)和类私有(两个下划线)混合变量。

Python教程 » » 私有变量

This is an ancient question, but both module private (one underscore) and class-private (two underscores) mangled variables are now covered in the standard documentation:

The Python Tutorial » Classes » Private Variables


回答 6

嵌入闭包或函数是一种方法。这在JS中很常见,但非浏览器平台或浏览器工作程序则不需要。

在Python中,这似乎有些奇怪,但是如果确实需要隐藏某些东西,那可能就是这样。更重要的是,使用python API并保留需要隐藏在C(或其他语言)中的内容可能是最好的方法。如果没有,我会将代码放入函数中,调用该函数并使它返回要导出的项目。

embedded with closures or functions is one way. This is common in JS although not required for non-browser platforms or browser workers.

In Python it seems a bit strange, but if something really needs to be hidden than that might be the way. More to the point using the python API and keeping things that require to be hidden in the C (or other language) is probably the best way. Failing that I would go for putting the code inside a function, calling that and having it return the items you want to export.


回答 7

Python具有三种模式,分别是private,public和protected。在导入模块时,只能访问public模式。因此,不能从模块外部(即在导入时)调用private和protected模块。

Python has three modes via., private, public and protected .While importing a module only public mode is accessible .So private and protected modules cannot be called from outside of the module i.e., when it is imported .


Python在类中是否具有“私有”变量?

问题:Python在类中是否具有“私有”变量?

我来自Java世界,正在阅读Bruce Eckels的Python 3 Patterns,Recipes和Idioms

在阅读类时,它继续说在Python中不需要声明实例变量。您只需在构造函数中使用它们,然后它们就在那里。

因此,例如:

class Simple:
    def __init__(self, s):
        print("inside the simple constructor")
        self.s = s

    def show(self):
        print(self.s)

    def showMsg(self, msg):
        print(msg + ':', self.show())

如果是这样,那么类的任何对象都Simple可以s在类外部更改变量的值。

例如:

if __name__ == "__main__":
    x = Simple("constructor argument")
    x.s = "test15" # this changes the value
    x.show()
    x.showMsg("A message")

在Java中,我们已经学会了有关公共/私有/保护变量的知识。这些关键字很有意义,因为有时您需要一个类中的变量,而该类之外的任何人都无法访问该变量。

为什么在Python中不需要这样做?

I’m coming from the Java world and reading Bruce Eckels’ Python 3 Patterns, Recipes and Idioms.

While reading about classes, it goes on to say that in Python there is no need to declare instance variables. You just use them in the constructor, and boom, they are there.

So for example:

class Simple:
    def __init__(self, s):
        print("inside the simple constructor")
        self.s = s

    def show(self):
        print(self.s)

    def showMsg(self, msg):
        print(msg + ':', self.show())

If that’s true, then any object of class Simple can just change the value of variable s outside of the class.

For example:

if __name__ == "__main__":
    x = Simple("constructor argument")
    x.s = "test15" # this changes the value
    x.show()
    x.showMsg("A message")

In Java, we have been taught about public/private/protected variables. Those keywords make sense because at times you want variables in a class to which no one outside the class has access to.

Why is that not required in Python?


回答 0

这是文化的。在Python中,您无需写入其他类的实例或类变量。在Java中,如果您真的想做的话,什么也不能阻止您做同样的事情-毕竟,您始终可以编辑类本身的源代码以达到相同的效果。Python放弃了这种安全性的幌子,并鼓励程序员负责。实际上,这非常好用。

如果出于某种原因要模拟私有变量,则始终可以使用PEP 8中__前缀。Python会像这样对变量名称进行修饰,以使它们在包含它们的类之外的代码中不易被看到(尽管只要有足够的决心,您可以解决它,就像您可以使用它来避开Java保护一样)。__foo

按照相同的约定,即使没有从技术上阻止您这样做_前缀也意味着不要离开。您不会玩弄看起来像__foo或的另一个类的变量_bar

It’s cultural. In Python, you don’t write to other classes’ instance or class variables. In Java, nothing prevents you from doing the same if you really want to – after all, you can always edit the source of the class itself to achieve the same effect. Python drops that pretence of security and encourages programmers to be responsible. In practice, this works very nicely.

If you want to emulate private variables for some reason, you can always use the __ prefix from PEP 8. Python mangles the names of variables like __foo so that they’re not easily visible to code outside the class that contains them (although you can get around it if you’re determined enough, just like you can get around Java’s protections if you work at it).

By the same convention, the _ prefix means stay away even if you’re not technically prevented from doing so. You don’t play around with another class’s variables that look like __foo or _bar.


回答 1

python中的私有变量或多或少是一种技巧:解释器故意重命名该变量。

class A:
    def __init__(self):
        self.__var = 123
    def printVar(self):
        print self.__var

现在,如果您尝试__var在类定义之外进行访问,它将失败:

 >>>x = A()
 >>>x.__var # this will return error: "A has no attribute __var"

 >>>x.printVar() # this gives back 123

但是您可以轻松地摆脱这一点:

 >>>x.__dict__ # this will show everything that is contained in object x
               # which in this case is something like {'_A__var' : 123}

 >>>x._A__var = 456 # you now know the masked name of private variables
 >>>x.printVar() # this gives back 456

您可能知道OOP中的方法是这样调用的:x.printVar() => A.printVar(x),如果A.printVar()可以访问中的某个字段,那么x也可以在外部 访问该字段A.printVar()…毕竟,创建函数是为了可重用性,内部的语句没有特殊的功能。

当涉及到编译器时,游戏就不同了(隐私是编译器级别的概念)。它知道具有访问控制修饰符的类定义,因此如果在编译时未遵循规则,则可能会出错

Private variables in python is more or less a hack: the interpreter intentionally renames the variable.

class A:
    def __init__(self):
        self.__var = 123
    def printVar(self):
        print self.__var

Now, if you try to access __var outside the class definition, it will fail:

 >>>x = A()
 >>>x.__var # this will return error: "A has no attribute __var"

 >>>x.printVar() # this gives back 123

But you can easily get away with this:

 >>>x.__dict__ # this will show everything that is contained in object x
               # which in this case is something like {'_A__var' : 123}

 >>>x._A__var = 456 # you now know the masked name of private variables
 >>>x.printVar() # this gives back 456

You probably know that methods in OOP are invoked like this: x.printVar() => A.printVar(x), if A.printVar() can access some field in x, this field can also be accessed outside A.printVar()…after all, functions are created for reusability, there is no special power given to the statements inside.

The game is different when there is a compiler involved (privacy is a compiler level concept). It know about class definition with access control modifiers so it can error out if the rules are not being followed at compile time


回答 2

正如上面的许多评论所正确提到的,我们不要忘记访问修饰符的主要目标:帮助代码用户理解应该更改的内容和不应该更改的内容。当您看到一个私有字段时,您不会把它弄乱。因此,主要是语法糖,可以通过_和__在Python中轻松实现。

As correctly mentioned by many of the comments above, let’s not forget the main goal of Access Modifiers: To help users of code understand what is supposed to change and what is supposed not to. When you see a private field you don’t mess around with it. So it’s mostly syntactic sugar which is easily achieved in Python by the _ and __.


回答 3

下划线约定中存在私有变量的变体。

In [5]: class Test(object):
   ...:     def __private_method(self):
   ...:         return "Boo"
   ...:     def public_method(self):
   ...:         return self.__private_method()
   ...:     

In [6]: x = Test()

In [7]: x.public_method()
Out[7]: 'Boo'

In [8]: x.__private_method()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-fa17ce05d8bc> in <module>()
----> 1 x.__private_method()

AttributeError: 'Test' object has no attribute '__private_method'

有一些细微的差异,但是出于编程模式思想纯净的考虑,其足够好。

@private装饰器中有一些示例可以更紧密地实现该概念,但是可以使用YMMV。可以说也可以编写一个使用meta的类定义

There is a variation of private variables in the underscore convention.

In [5]: class Test(object):
   ...:     def __private_method(self):
   ...:         return "Boo"
   ...:     def public_method(self):
   ...:         return self.__private_method()
   ...:     

In [6]: x = Test()

In [7]: x.public_method()
Out[7]: 'Boo'

In [8]: x.__private_method()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-fa17ce05d8bc> in <module>()
----> 1 x.__private_method()

AttributeError: 'Test' object has no attribute '__private_method'

There are some subtle differences, but for the sake of programming pattern ideological purity, its good enough.

There are examples out there of @private decorators that more closely implement the concept, but YMMV. Arguably one could also write a class defintion that uses meta


回答 4

“在Java中,我们被教导有关公共/私有/保护变量”

“为什么在python中不需要?”

出于同样的原因,在Java中不需要

您可以自由使用-或不使用privateand protected

作为一个Python和Java程序员,我发现,privateprotected是非常,非常重要的设计理念。但实际上,在成千上万的Java和Python行中,我从未真正使用过privateprotected

为什么不?

这是我的问题“不受谁保护?”

我团队中的其他程序员?他们有出处。受保护的人何时可以更改它意味着什么?

其他团队的其他程序员?他们在同一家公司工作。他们可以-通过电话-获取消息来源。

客户?(通常)是按需租用的程序。客户(通常)拥有代码。

那么,到底是谁在保护我?

“In java, we have been taught about public/private/protected variables”

“Why is that not required in python?”

For the same reason, it’s not required in Java.

You’re free to use — or not use private and protected.

As a Python and Java programmer, I’ve found that private and protected are very, very important design concepts. But as a practical matter, in tens of thousands of lines of Java and Python, I’ve never actually used private or protected.

Why not?

Here’s my question “protected from whom?”

Other programmers on my team? They have the source. What does protected mean when they can change it?

Other programmers on other teams? They work for the same company. They can — with a phone call — get the source.

Clients? It’s work-for-hire programming (generally). The clients (generally) own the code.

So, who — precisely — am I protecting it from?


回答 5

如前所述,您可以通过在变量或方法前加上下划线作为前缀来表明该变量或方法是私有的。如果您觉得不够,可以随时使用property装饰器。这是一个例子:

class Foo:

    def __init__(self, bar):
        self._bar = bar

    @property
    def bar(self):
        """Getter for '_bar'."""
        return self._bar

这样,引用的某人或某物bar实际上是在引用bar函数的返回值,而不是变量本身,因此可以访问但不能更改。但是,如果有人真的想要,他们可以简单地使用_bar并为其分配新的值。就像反复提到的那样,没有一种万无一失的方法可以防止某人访问您想要隐藏的变量和方法。但是,使用property可以发送的最清晰的消息是不要编辑变量。property也可以用于更复杂的getter / setter / deleter访问路径,如下所示:https : //docs.python.org/3/library/functions.html#property

As mentioned earlier, you can indicate that a variable or method is private by prefixing it with an underscore. If you don’t feel like this is enough, you can always use the property decorator. Here’s an example:

class Foo:

    def __init__(self, bar):
        self._bar = bar

    @property
    def bar(self):
        """Getter for '_bar'."""
        return self._bar

This way, someone or something that references bar is actually referencing the return value of the bar function rather than the variable itself, and therefore it can be accessed but not changed. However, if someone really wanted to, they could simply use _bar and assign a new value to it. There is no surefire way to prevent someone from accessing variables and methods that you wish to hide, as has been said repeatedly. However, using property is the clearest message you can send that a variable is not to be edited. property can also be used for more complex getter/setter/deleter access paths, as explained here: https://docs.python.org/3/library/functions.html#property


回答 6

Python通过自动将类名添加到以两个下划线开头的任何标识符的功能,对私有标识符的支持有限。在大多数情况下,这对程序员是透明的,但是最终结果是,以此方式命名的任何变量都可以用作私有变量。

有关更多信息,请参见此处

通常,与其他语言相比,Python的面向对象的实现有点原始。但实际上,我很喜欢。从概念上讲,这是一种非常简单的实现,非常适合该语言的动态样式。

Python has limited support for private identifiers, through a feature that automatically prepends the class name to any identifiers starting with two underscores. This is transparent to the programmer, for the most part, but the net effect is that any variables named this way can be used as private variables.

See here for more on that.

In general, Python’s implementation of object orientation is a bit primitive compared to other languages. But I enjoy this, actually. It’s a very conceptually simple implementation and fits well with the dynamic style of the language.


回答 7

我唯一使用私有变量的时间是在写入或读取变量时需要做其他事情时,因此需要强制使用setter和/或getter。

如前所述,这再次涉及文化。我一直在从事免费阅读和编写其他类变量的项目。一个实现被弃用时,识别使用该功能的所有代码路径的时间要长得多。当强制使用setter和getter时,可以很容易地编写一条调试语句来识别已调用了不赞成使用的方法以及调用该方法的代码路径。

当您在任何人都可以编写扩展的项目上时,通知用户有关已弃用的方法的信息,这些方法将在几个发行版中消失,因此对于将升级时模块的损坏降至最低至关重要。

所以我的答案是;如果您和您的同事维护一个简单的代码集,那么保护类变量并非总是必要的。如果您正在编写一个可扩展的系统,那么对内核进行的更改就变得势在必行,而所有的扩展都需要使用代码来捕获这些更改。

The only time I ever use private variables is when I need to do other things when writing to or reading from the variable and as such I need to force the use of a setter and/or getter.

Again this goes to culture, as already stated. I’ve been working on projects where reading and writing other classes variables was free-for-all. When one implementation became deprecated it took a lot longer to identify all code paths that used that function. When use of setters and getters was forced, a debug statement could easily be written to identify that the deprecated method had been called and the code path that calls it.

When you are on a project where anyone can write an extension, notifying users about deprecated methods that are to disappear in a few releases hence is vital to keep module breakage at a minimum upon upgrades.

So my answer is; if you and your colleagues maintain a simple code set then protecting class variables is not always necessary. If you are writing an extensible system then it becomes imperative when changes to the core is made that needs to be caught by all extensions using the code.


回答 8

私有和受保护的概念非常重要。但是python-只是用于原型开发和快速开发的工具,可用于开发的资源有限,这就是为什么在python中并没有严格遵循某些保护级别的原因。您可以在类成员中使用“ __”,它可以正常工作,但看起来不够好-每次访问此类字段都包含这些字符。

另外,您会注意到python OOP概念并不完美,smaltalk或ruby更接近于纯OOP概念。甚至C#或Java都更接近。

Python是非常好的工具。但是它是简化的OOP语言。从语法和概念上简化。python存在的主要目的是使开发人员能够以非常快的方式编写具有高抽象级别的易读代码。

private and protected concepts are very important. But python – just a tool for prototyping and rapid development with restricted resources available for development, that is why some of protection levels are not so strict followed in python. You can use “__” in class member, it works properly, but looks not good enough – each access to such field contains these characters.

Also, you can noticed that python OOP concept is not perfect, smaltalk or ruby much closer to pure OOP concept. Even C# or Java are closer.

Python is very good tool. But it is simplified OOP language. Syntactically and conceptually simplified. The main goal of python existence is to bring to developers possibility to write easy readable code with high abstraction level in a very fast manner.


回答 9

抱歉,“恢复”线程,但是,我希望这会对某人有所帮助:

在Python3中,如果您只想“封装”类属性(例如在Java中),则可以执行以下操作:

class Simple:
    def __init__(self, str):
        print("inside the simple constructor")
        self.__s = str

    def show(self):
        print(self.__s)

    def showMsg(self, msg):
        print(msg + ':', self.show())

要实例化此操作,请执行以下操作:

ss = Simple("lol")
ss.show()

注意:print(ss.__s)会抛出错误。

实际上,Python3将混淆全局属性名称。像在Java中一样,将其变为“私有”属性。该属性的名称仍然是全局的,但是以一种无法访问的方式,就像其他语言中的私有属性一样。

但是不要害怕。没关系 它也做这项工作。;)

Sorry guys for “resurrecting” the thread, but, I hope this will help someone:

In Python3 if you just want to “encapsulate” the class attributes, like in Java, you can just do the same thing like this:

class Simple:
    def __init__(self, str):
        print("inside the simple constructor")
        self.__s = str

    def show(self):
        print(self.__s)

    def showMsg(self, msg):
        print(msg + ':', self.show())

To instantiate this do:

ss = Simple("lol")
ss.show()

Note that: print(ss.__s) will throw an error.

In practice, Python3 will obfuscate the global attribute name. Turning this like a “private” attribute, like in Java. The attribute’s name is still global, but in an inaccessible way, like a private attribute in other languages.

But don’t be afraid of it. It doesn’t matter. It does the job too. ;)


回答 10

Python没有像C ++或Java那样的任何私有变量。如果需要,您也可以随时访问任何成员变量。但是,在Python中不需要私有变量,因为在Python中公开类成员变量也不错。如果需要封装成员变量,则可以稍后使用“ @property”来实现,而无需破坏现有的客户端代码。

在python中,单个下划线“ _”用于表示方法或变量不被视为类的公共api的一部分,并且该api的这一部分可以在不同版本之间进行更改。您可以使用这些方法/变量,但如果使用此类的较新版本,则代码可能会中断。

双下划线“ __”并不表示“私有变量”。您可以使用它来定义“局部类”的变量,并且这些变量不能轻易被子类覆盖。它处理变量名称。

例如:

class A(object):
    def __init__(self):
        self.__foobar = None # will be automatically mangled to self._A__foobar

class B(A):
    def __init__(self):
        self.__foobar = 1 # will be automatically mangled to self._B__foobar

self .__ foobar的名称会在A类中自动更改为self._A__foobar。在B类中,其名称将更改为self._B__foobar。因此,每个子类都可以定义自己的变量__foobar而不覆盖其父变量。但是没有什么可以阻止您访问以双下划线开头的变量。但是,名称修改可防止您偶然调用此变量/方法。

我强烈建议观看Raymond Hettingers谈论Pycon 2013上的“ Pythons类开发工具包”(应该在Youtube上提供),该示例很好地说明了为什么以及如何使用@property和“ __”-instance变量。

Python does not have any private variables like C++ or Java does. You could access any member variable at any time if wanted, too. However, you don’t need private variables in Python, because in Python it is not bad to expose your classes member variables. If you have the need to encapsulate a member variable, you can do this by using “@property” later on without breaking existing client code.

In python the single underscore “_” is used to indicate, that a method or variable is not considered as part of the public api of a class and that this part of the api could change between different versions. You can use these methods/variables, but your code could break, if you use a newer version of this class.

The double underscore “__” does not mean a “private variable”. You use it to define variables which are “class local” and which can not be easily overidden by subclasses. It mangles the variables name.

For example:

class A(object):
    def __init__(self):
        self.__foobar = None # will be automatically mangled to self._A__foobar

class B(A):
    def __init__(self):
        self.__foobar = 1 # will be automatically mangled to self._B__foobar

self.__foobar’s name is automatically mangled to self._A__foobar in class A. In class B it is mangled to self._B__foobar. So every subclass can define its own variable __foobar without overriding its parents variable(s). But nothing prevents you from accessing variables beginning with double underscores. However, name-mangling prevents you from calling this variables /methods incidentally.

I strongly recommend to watch Raymond Hettingers talk “Pythons class development toolkit” from Pycon 2013 (should be available on Youtube), which gives a good example why and how you should use @property and “__”-instance variables.


回答 11

实际上,您可以C#使用以下简单技巧来模拟吸气剂和吸气剂:

class Screen(object):

    def getter_setter_y(self, y, get=True):
        if get is True:
            Screen.getter_setter_y.value = y
        else:
            return Screen.getter_setter_y.value

     def getter_setter_x(self, x, get=True):
         if get is True:
             Screen.getter_setter_x.value = x
         else:
             return Screen.getter_setter_x.value

然后像这样使用它C#

scr = Screen()
scr.getter_setter_x(100)
value =  scr.getter_setter_x(0, get=False)
print (value)

这只是在函数中声明一个静态局部变量,该变量将扮演获取/设置的角色,因为这是通过get和set方法共享变量的唯一方法,而无需将其全局化为类或文件。

Actually you can simulate a C# getter and setter using this simple trick:

class Screen(object):

    def getter_setter_y(self, y, get=True):
        if get is True:
            Screen.getter_setter_y.value = y
        else:
            return Screen.getter_setter_y.value

     def getter_setter_x(self, x, get=True):
         if get is True:
             Screen.getter_setter_x.value = x
         else:
             return Screen.getter_setter_x.value

Then use it similar like in C#:

scr = Screen()
scr.getter_setter_x(100)
value =  scr.getter_setter_x(0, get=False)
print (value)

It’s just declaring a static local variable in a function that will play a get/set role, since that’s the only way to share a variable via get and set methods, without make it global for a class or file.