与经典的getter+setter相比,@property表示法有什么优点?在哪些特定的情况下,程序员应该选择使用其中一种而不是另一种?

属性:

class MyClass(object):
    @property
    def my_attr(self):
        return self._my_attr

    @my_attr.setter
    def my_attr(self, value):
        self._my_attr = value

没有属性:

class MyClass(object):
    def get_my_attr(self):
        return self._my_attr

    def set_my_attr(self, value):
        self._my_attr = value

当前回答

喜欢的属性。这就是他们存在的意义。

原因是Python中的所有属性都是公共的。以下划线或两个下划线开头的名称只是一个警告,说明给定的属性是一个实现细节,在将来的代码版本中可能不会保持相同。它不会阻止您实际获取或设置该属性。因此,标准属性访问是访问属性的正常的python方式。

属性的优点是它们在语法上与属性访问相同,因此您可以在不更改客户机代码的情况下从一个属性更改到另一个属性。您甚至可以有一个版本的类使用属性(例如,用于契约代码或调试),而另一个版本的类不用于生产,而不需要更改使用它的代码。与此同时,您不必为所有内容编写getter和setter,以防以后可能需要更好地控制访问。

其他回答

对我来说,使用属性更直观,更适合大多数代码。

比较

o.x = 5
ox = o.x

vs.

o.setX(5)
ox = o.getX()

对我来说很明显,更容易理解。此外,属性允许私有变量更容易。

@property和传统的getter和setter都有各自的优点。这取决于您的用例。

@property的优点

You don't have to change the interface while changing the implementation of data access. When your project is small, you probably want to use direct attribute access to access a class member. For example, let's say you have an object foo of type Foo, which has a member num. Then you can simply get this member with num = foo.num. As your project grows, you may feel like there needs to be some checks or debugs on the simple attribute access. Then you can do that with a @property within the class. The data access interface remains the same so that there is no need to modify client code. Cited from PEP-8: For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods. Keep in mind that Python provides an easy path to future enhancement, should you find that a simple data attribute needs to grow functional behavior. In that case, use properties to hide functional implementation behind simple data attribute access syntax. Using @property for data access in Python is regarded as Pythonic: It can strengthen your self-identification as a Python (not Java) programmer. It can help your job interview if your interviewer thinks Java-style getters and setters are anti-patterns.

传统getter和setter的优点

Traditional getters and setters allow for more complicated data access than simple attribute access. For example, when you are setting a class member, sometimes you need a flag indicating where you would like to force this operation even if something doesn't look perfect. While it is not obvious how to augment a direct member access like foo.num = num, You can easily augment your traditional setter with an additional force parameter: def Foo: def set_num(self, num, force=False): ... Traditional getters and setters make it explicit that a class member access is through a method. This means: What you get as the result may not be the same as what is exactly stored within that class. Even if the access looks like a simple attribute access, the performance can vary greatly from that. Unless your class users expect a @property hiding behind every attribute access statement, making such things explicit can help minimize your class users surprises. As mentioned by @NeilenMarais and in this post, extending traditional getters and setters in subclasses is easier than extending properties. Traditional getters and setters have been widely used for a long time in different languages. If you have people from different backgrounds in your team, they look more familiar than @property. Also, as your project grows, if you may need to migrate from Python to another language that doesn't have @property, using traditional getters and setters would make the migration smoother.

警告

Neither @property nor traditional getters and setters makes the class member private, even if you use double underscore before its name: class Foo: def __init__(self): self.__num = 0 @property def num(self): return self.__num @num.setter def num(self, num): self.__num = num def get_num(self): return self.__num def set_num(self, num): self.__num = num foo = Foo() print(foo.num) # output: 0 print(foo.get_num()) # output: 0 print(foo._Foo__num) # output: 0

简单的答案是:properties轻松获胜。总是这样。

有时需要getter和setter,但即使这样,我也会将它们“隐藏”到外部世界。在Python中有很多方法可以做到这一点(getattr, setattr, __getattribute__,等等…,但最简洁明了的是:

def set_email(self, value):
    if '@' not in value:
        raise Exception("This doesn't look like an email address.")
    self._email = value

def get_email(self):
    return self._email

email = property(get_email, set_email)

下面是一篇简短的文章,介绍Python中的getter和setter主题。

喜欢的属性。这就是他们存在的意义。

原因是Python中的所有属性都是公共的。以下划线或两个下划线开头的名称只是一个警告,说明给定的属性是一个实现细节,在将来的代码版本中可能不会保持相同。它不会阻止您实际获取或设置该属性。因此,标准属性访问是访问属性的正常的python方式。

属性的优点是它们在语法上与属性访问相同,因此您可以在不更改客户机代码的情况下从一个属性更改到另一个属性。您甚至可以有一个版本的类使用属性(例如,用于契约代码或调试),而另一个版本的类不用于生产,而不需要更改使用它的代码。与此同时,您不必为所有内容编写getter和setter,以防以后可能需要更好地控制访问。

在Python中,你不会仅仅为了好玩而使用getter或setter或属性。首先只使用属性,然后在需要时,最终迁移到属性,而不必使用类更改代码。

确实有很多带有.py扩展名的代码在任何地方都使用getter和setter、继承和无意义的类,例如一个简单的元组就可以了,但这是人们使用Python用c++或Java编写的代码。

这不是Python代码。