与任意取代属性名称创建Python类 [英] creating Python classes with arbitrarily substituted attribute name

查看:108
本文介绍了与任意取代属性名称创建Python类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不给这个问题一个更好的标题表示歉意;那我张贴它的原因是,我甚至不正确的术语,知道我所期待的。

I apologize for not giving this question a better title; the reason that I am posting it is that I don't even have the correct terminology to know what I am looking for.

我已经定义与属性垃圾邮件一类:

I have defined a class with an attribute 'spam':


def SpamClass(object):
    def __init__(self, arg):
        self.spam = arg
    def __str__(self):
        return self.spam

我想创建一个具有完全相同的功能类,但与命名,而不是'垃圾邮件'蛋'的属性(次/兄弟姐妹?):

I want to create a (sub/sibling?)class that has exactly the same functionality, but with an attribute named 'eggs' instead of 'spam':


def EggsClass(object):
    def __init__(self, arg):
        self.eggs = arg
    def __str__(self):
        return self.eggs

要概括,我怎么创建功能,相同的类任意属性名称?上课的时候​​有复杂的行为,似乎傻复制code。

To generalize, how do I create functionally-identical classes with arbitrary attribute names? When the class has complicated behavior, it seems silly to duplicate code.

更新:我同意这闻起来像糟糕的设计。为了澄清,我并不想在这个愚蠢的方式来解决特定的问题。我只是想知道如何随意命名(非魔法)的对象的 __ __字典,而preserving功能的内容。考虑类似的键()方法字典状物体。人们创建各种类与键,按照约定的行为()方法和命名约定是一件好事。但名称是任意的。我怎样才能使一个类与垃圾邮件()方法正是取代键()无需手动替换/按键/垃圾邮件/源?

Update: I agree that this smells like bad design. To clarify, I'm not trying to solve a particular problem in this stupid way. I just want to know how to arbitrarily name the (non-magic) contents of an object's __dict__ while preserving functionality. Consider something like the keys() method for dict-like objects. People create various classes with keys() methods that behave according to convention, and the naming convention is a Good Thing. But the name is arbitrary. How can I make a class with a spam() method that exactly replaces keys() without manually substituting /keys/spam/ in the source?

重载 __ __ GETATTR 和朋友们引用通用属性看起来不太优雅而脆给我。如果一个子类重新实现这些方法中,必须适应这种行为。我宁愿把它显示给用户,有一个简单的基类,可以天真地访问一个名为属性。

Overloading __getattr__ and friends to reference the generic attribute seems inelegant and brittle to me. If a subclass reimplements these methods, it must accommodate this behavior. I would rather have it appear to the user that there is simply a base class with a named attribute that can be accessed naively.

其实,我能想到的一个合理的用例。假设你想赋予一个特殊的属性和操作或依赖于这个属性一些密切相关的方法mixin类。用户可能希望以不同命名这个特殊的属性不同类别的(在现实世界中的问题域匹配的名称或以避免名称冲突),而重用的基本行为。

Actually, I can think of a plausible use case. Suppose that you want a mixin class that confers a special attribute and some closely related methods that manipulate or depend upon this attribute. A user may want to name this special attribute differently for different classes (to match names in the real-world problem domain or to avoid name collisions) while reusing the underlying behavior.

推荐答案

下面是一个办法让我觉得你想要的效果。

Here is a way to get the effect I think you want.

定义泛型类具有通用属性名称。然后在每个子类按照 HTTP的建议://docs.python。组织/参考/ datamodel.html#定制属性访问,使属性看起来像外部它被称为无论你想叫。

Define a generic class with a generic attribute name. Then in each sub class follow the advice in http://docs.python.org/reference/datamodel.html#customizing-attribute-access to make the attribute look externally like it is called whatever you want it called.

您的你做什么的描述感觉它有一个code闻对我来说,我建议非常谨慎审查您的设计,看看这是否是你想要做的真的是。但是,你可以把它与我的建议工作。

Your description of what you do feels like it has a "code smell" to me, I'd suggest reviewing your design very carefully to see whether this is really what you want to do. But you can make it work with my suggestion.

这篇关于与任意取代属性名称创建Python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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