在Python中访问过正当的吗? [英] Are accessors in Python ever justified?

查看:109
本文介绍了在Python中访问过正当的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认识到,在大多数情况下,它是用Python pferred刚刚获得$ P $属性直接,因为有封装没有真正的概念像有Java和等。不过,我想知道如果没有任何异常,特别是有不同的实现的抽象类。

比方说,我写了一堆抽象类(因为我),他们再与像仓库和修订版本控制系统做(因为他们做的)present的事情。像SvnRevision和HgRevision和GitRevision的东西有很密切的语义联系在一起的,我希望他们能够做同样的事(这样我就可以有code别处作用于任何类型的存储库的对象,并且是不可知的子类)的,这就是为什么我希望他们能够从抽象类继承。但是,它们的实现有很大的不同。

到目前为止,已实施股很多的属性名称,并在很多code类本身以外的子类,使用直接访问属性。例如,修订的每个子类都有一个作者属性,日期属性,等等。然而,属性不是任何地方的抽象类描述。这似乎对我来说,一个非常脆弱的设计。

如果有人想编写修订类的另一种实现方式,我觉得他们应该能够只是看着抽象类这样做。但是,满足所有的抽象方法几乎肯定会失败,因为作者不知道他们需要的属性称为作家和日期等类的实现,因此code,试图访问Revision.author会抛出异常。也许不难找到问题的根源,但仍然有刺激性,它只是感觉像一个不雅的设计。

我的解决办法是写访问的抽象类(get_id,get_author等)的方法。我认为这实际上是pretty干净的解决方案,因为它消除了对属性的命名和存储任意限制,只是明确了对象需要能够访问哪些数据。实现所有的抽象类方法中的任何类将工作...感觉不错。

不管怎么说,球队我与恨的工作该解决方案(貌似该访问是unpythonic的原因,我真的不能与争论)。所以...有什么选择?文档?或者是我想象一个非问题的问题?

请注意:我考虑过的属性,但我不认为他们是一个清洁的解决方案


解决方案

  

请注意:我考虑过的属性,但我不认为他们是一个清洁的解决方案


但他们。通过使用属性,你有你想要的类签名,而能够使用的属性作为属性本身

 高清_get_id(个体经营):
   回报self._id高清_set_id(个体经营,newid的):
   self._id = NEWID

很可能类似于你现在所拥有的。为了安抚你的团队,你只需要添加以下内容:

  ID =财产(_get_id,_set_id)

您也可以使用属性作为装饰:

  @property
DEF ID(个体经营):
    回报self._id@ id.setter
DEF ID(个体经营,newid的):
    self._id = NEWID

,并使其只读,只留下了 set_id /的 id.setter 位。

I realize that in most cases, it's preferred in Python to just access attributes directly, since there's no real concept of encapsulation like there is in Java and the like. However, I'm wondering if there aren't any exceptions, particularly with abstract classes that have disparate implementations.

Let's say I'm writing a bunch of abstract classes (because I am) and that they represent things having to do with version control systems like repositories and revisions (because they do). Something like an SvnRevision and an HgRevision and a GitRevision are very closely semantically linked, and I want them to be able to do the same things (so that I can have code elsewhere that acts on any kind of Repository object, and is agnostic of the subclass), which is why I want them to inherit from an abstract class. However, their implementations vary considerably.

So far, the subclasses that have been implemented share a lot of attribute names, and in a lot of code outside of the classes themselves, direct attribute access is used. For example, every subclass of Revision has an author attribute, and a date attribute, and so on. However, the attributes aren't described anywhere in the abstract class. This seems to me like a very fragile design.

If someone wants to write another implementation of the Revision class, I feel like they should be able to do so just by looking at the abstract class. However, an implementation of the class that satisfies all of the abstract methods will almost certainly fail, because the author won't know that they need attributes called 'author' and 'date' and so on, so code that tries to access Revision.author will throw an exception. Probably not hard to find the source of the problem, but irritating nonetheless, and it just feels like an inelegant design.

My solution was to write accessor methods for the abstract classes (get_id, get_author, etc.). I thought this was actually a pretty clean solution, since it eliminates arbitrary restrictions on how attributes are named and stored, and just makes clear what data the object needs to be able to access. Any class that implements all of the methods of the abstract class will work... that feels right.

Anyways, the team I'm working with hates this solution (seemingly for the reason that accessors are unpythonic, which I can't really argue with). So... what's the alternative? Documentation? Or is the problem I'm imagining a non-issue?

Note: I've considered properties, but I don't think they're a cleaner solution.

解决方案

Note: I've considered properties, but I don't think they're a cleaner solution.

But they are. By using properties, you'll have the class signature you want, while being able to use the property as an attribute itself.

def _get_id(self):
   return self._id

def _set_id(self, newid):
   self._id = newid

Is likely similar to what you have now. To placate your team, you'd just need to add the following:

id = property(_get_id, _set_id)

You could also use property as a decorator:

@property
def id(self):
    return self._id

@id.setter
def id(self, newid):
    self._id = newid

And to make it readonly, just leave out set_id/the id.setter bit.

这篇关于在Python中访问过正当的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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