Python中的__set__和__setattr__有什么区别?何时使用? [英] What is the difference between __set__ and __setattr__ in Python and when should which be used?

查看:149
本文介绍了Python中的__set__和__setattr__有什么区别?何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述.

来自Java即时通讯,用于:

Coming from Java im used to:

private int A;

public void setA(int A) {
    this.A = A;
}

public int getA() {
    return this.A
}

如何在Python中做到这一点(如果需要).如果使用 __ setattr __ __ set __ 中的一个,另一个用于什么?

How do I do that (if I need to) in Python. And if one of __setattr__ or __set__ is used for this, what is the other one used for?

我觉得我需要澄清.我知道在Python中,doe不需要先创建setter和getter.

I feel I need to clarify. I know that in Python one doe's not create setters and getters before they are needed.

让我们说我想做这样的事情:

Lets say I want to do something like this:

public void setA(int A) {
    update_stuff(A);
    and_calculate_some_thing(A);
    this.A = A;
}

实现此目标的"pythonic"方式是什么?

What is the "pythonic" way to implement this?

推荐答案

在python中,应该使用 property 实现这种类似 的操作(只有在这样做时,一些有用的东西).

In python, something like this should be implemented using a property (and then only when they do something useful).

class Foo(object):
    def __init__(self):
        self._x = None

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

    @x.setter
    def x(self,y):
        self._x = y

在此示例中,最好做一下(爱德华指出):

In this example, it would be better to just do (as pointed out by Edward):

class Foo(object):
     def __init__(self):
         self.x = None

因为我们的getter/setter方法实际上不做任何事情...但是,当setter/getter实际上所做的事情不仅仅是分配/返回属性值时,属性就变得非常有用.

since our getter/setter methods don't actually do anything ... However, properties become very useful when the setter/getter actually does something more than just assign/return an attribute's value.

它也可以使用 __ setattr __ / __ getattr __ 来实现(但不应以这种方式实现,因为如果您的类具有1个以上的属性,它将很快变得很麻烦.我还猜想,用这种方法比使用属性要慢):

It could also be implemented using __setattr__/__getattr__ (but it shouldn't be implemented this way as it quickly becomes cumbersome if your class has more than 1 property. I would also guess that doing it this way would be slower than using properties):

class Foo(object):
    def __init__(self):
        self._x = None
    def __setattr__(self,attr,obj):
        if(attr == 'x'):
            object.__setattr__(self,'_x',obj)
        else:
            object.__setattr__(self,attr,obj)

    def __getattr__(self,attr):
        if(attr == 'x'):
            return object.__getattr__(self,'_x')
        else:
            return object.__getattr__(self,attr)

__ setattr __ __ getattr __ 的实际作用而言... __ setattr __ / __ getattr __ 是执行以下操作时所要调用的:

In terms of what __setattr__ and __getattr__ actually do... __setattr__/__getattr__ are what are called when you do something like:

myclassinstance = MyClass()
myclassinstance.x = 'foo'  #translates to MyClass.__setattr__(myclassinstance,'x','foo')
bar = myclassinstance.x    #translates to bar=MyClass.__getattr__(myclassinstance,'x')

对于 __ get __ __ set __ :

As for __get__ and __set__: previous posts have discussed that quite nicely.

请注意,在python 中,没有诸如私有变量之类的东西.通常,在一个类成员中加一个下划线为前缀,您不应将其弄乱(除非您当然知道自己在做什么).如果以2个下划线作为前缀,它将调用名称修改,这使得在类外部进行访问变得更加困难.这用于防止继承中的名称空间冲突(通常也不要弄混那些变量).

Note that in python there is no such thing as private variables. In general, in a class member is prefixed with an underscore, you shouldn't mess with it (unless you know what you're doing of course). If it's prefixed with 2 underscores, it will invoke name-mangling which makes it harder to access outside the class. This is used to prevent namespace clashes in inheritance (and those variables are generally also not to be messed with).

这篇关于Python中的__set__和__setattr__有什么区别?何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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