两类具有结构上相同的方法,但一个名称不同的变量 [英] Two classes with a structurally identical method but one differently named variable

查看:147
本文介绍了两类具有结构上相同的方法,但一个名称不同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有公共函数 f 的类,如下所示:

I have two classes with a common function f, like here:

class ClassA(object):

    def f(self, var):
        self.A = g(var, self.A)

    # specific code for ClassA, also changing self.A

class ClassB(object):

    def f(self, var):
        self.B = g(var, self.B)

    # specific code for ClassB, also changing self.B

在两个类中, f 对名为 A 的两个变量执行相同的操作B ,它们在结构上是等效的。我想将 f 放入抽象类中,以避免代码重复。一种方法是

In both classes, f does the same on two variables called A and B, which are structurally equivalent. I would like to put f it into an abstract class to avoid code duplication. One way to do this is

class ClassX(object):

    def f(self, var):
        self.X = g(var, self.X)

class ClassA(ClassX):

    # specific code for ClassA, also changing self.X

class ClassB(ClassX):

    # specific code for ClassB, also changing self.X

在此解决方案中,变量 A B 已重命名 X 。但是,为了使我的代码更容易解​​释,我想保留这些特定名称(例如 A B ),用于特殊类中的 X

In this solution the variables A and B have been renamed X. However, to make my code more self-explaining, I would like to keep those specific names (say A,B) for X in the special classes.

有没有办法做到这一点?

Is there a way to do this?

如果您可以提出更有意义和更具描述性的标题,也请发表评论,以便对社区变得更有价值。

Also please comment if you can suggest a more meaningful and descriptive title, so it becomes more valuable to the community.

编辑:如果变量 A B 采用

推荐答案

首先,一个警告:如果您的案例实际上要求您描述的那种继承,则变量名称可能应该相同。在大多数情况下,为两个相关类中相同的属性赋予不同的名称并不能使代码更容易解​​释-使其变得更少易于解释。当属性实际上相同时,这会使属性看起来有所不同,从而增加了复杂性和混淆的可能性。

First, a caveat: if your case actually calls for inheritance of the kind you describe, then the variable name should probably be the same. In most cases, giving different names to attributes that are identical in two related classes doesn't make code more self-explaining -- it makes it less self-explaining. It makes the attributes look different when they're really the same, adding complexity and potential for confusion.

但是,我可以想象在某些情况下您可能想要做这样的事情-听起来有点像您想为两种不同的类型定义一个通用接口对象。一种简单的方法是使用属性定义别名。因此,在每个类中,您将具有不同的属性( A B ),但是您还需要定义一个属性 X ,在两种情况下都可以访问相应的属性。因此,在 ClassA 中,您将在类定义中执行以下操作:

However, I can imagine a few cases where you might want to do something like this -- it sounds a bit like you want to define a common interface to two different kinds of objects. One simple approach would be to use properties to define aliases; so in each class, you'd have different attributes (A and B), but you'd also define a property, X, that would access the appropriate attribute in either case. So in ClassA, you'd do something like this in the class definition:

@property
def X(self):
    return self.A
@x.setter
def X(self, value):
    self.A = value
@x.deleter
def X(self):
    del self.A

ClassB

@property
def X(self):
    return self.B
@x.setter
def X(self, value):
    self.B = value
@x.deleter
def X(self):
    del self.B

然后,您可以定义一个仅与 X 一起使用的通用函数,子类可以继承该函数,从而覆盖属性 X 以任何合适的方式。

Then, you could define a common function that would work only with X, which subclasses could inherit, overriding property X in whatever way is appropriate.

虽然这是很少值得的,但还需要额外的间接层。大多数时候,如果有充分的理由让两个类共享一个方法,则它们还应该共享该方法更改的属性,名称和全部。

This is rarely worth the additional layer of indirection though. Most of the time, if there's a good reason for two classes to share a method, then they should also share the attributes that the method changes, name and all.

这篇关于两类具有结构上相同的方法,但一个名称不同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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