自定义类的 Pythonic 方式

标签 python python-2.7

我正在尝试找出一种为自定义类使用继承的干净方法。我有以下目录结构。

inheritTest.py
Level1
    __init__.py
    t_Generic.py
Level2
    __init__.py
    t_Custom.py

目标:我希望通过导入 t_Custom 模块来访问 t_Generic 的所有类,并允许在 t_Custom 中进行所需的自定义。

上面的两个init.py 文件中没有任何内容。

t_Generic.py 包含一些通用类,如下所示:

class A(object):
    def __init__(self):
        self.hello = "Hello! I am Mr.A from t_generic"
    def someOtherMethod(self):
        pass

class B(object):
    def __init__(self):
        self.hello = "Hello! I am Mr.B from t_generic"
    def someOtherMethod(self):
        pass

class C(object):
    def __init__(self):
        self.hello = "Hello! I am Mr.C from t_generic"
    def changeHello(self):
        pass

实验t_Custom.py如下:

import Level1.t_Generic

#Override any Generic classes in this module.

#Option 1: Let's get Generic A so it lives in this scope as A
from Level1.t_Generic import A

#Option 2: Let's create B and inherit from Generic, make future custom changes here
class B(Level1.t_Generic.B):
    def __init__(self):
        super(B,self).__init__()


#I really want to inherit from C and change it's behavior
class C(Level1.t_Generic.C):
    def __init__(self):
        super(C,self).__init__()
    def changeHello(self):
        self.hello = "Hello! I am Mr.C modified in t_Custom"

问题:做这样的事情的Python方式是什么?我应该像选项 1 一样导入自定义模块中的所有泛型类,还是应该像选项 2 一样在自定义模块中创建继承的类并修改我想要继承的类?

inheritTest.py 中的示例用例:

import Level2.t_Custom

a = Level2.t_Custom.A()
b = Level2.t_Custom.B()
c = Level2.t_Custom.C()

print a.hello
print b.hello
print c.hello
c.changeHello()
print c.hello

输出:

Hello! I am Mr.A from t_generic
Hello! I am Mr.B from t_generic
Hello! I am Mr.C from t_generic
Hello! I am Mr.C modified in t_Custom

最佳答案

我还没有测试过这个,但我认为它应该能达到你想要的效果。考虑一下白板代码

首先,您需要在 t_Custom.py 中使用相对导入,然后导入泛型类

from ..Level1 import t_Generic
from t_Generic import A as GenericA
from t_Generic import B as GenericB
from t_Generic import C as GenericC

我重命名了这些类,以避免模块的命名空间中出现任何冲突。

现在,要在 t_Custom.py 中获得所需的输出(复制原始代码)

A = GenericA # Just re-export the generic A class

class B(GenericB):
    def __init__(self):
        super(B,self).__init__()

#I really want to inherit from C and change it's behavior
class C(GenericC):
    def __init__(self):
        super(C,self).__init__()
    def changeHello(self):
        self.hello = "Hello! I am Mr.C modified in t_Custom"

然后,尝试避免从模块导出 t_Generic.A 等

__all__ = ["A","B","C"]

正如 Alex 在评论中提到的那样,无意义地重写基类的方法通常被认为是“坏事”(TM)。仅当您想要修改或添加行为时才这样做。然后 t_Custom.py 变得更像这样:

from ..Level1 import t_Generic
from t_Generic import A as GenericA
from t_Generic import B as GenericB
from t_Generic import C as GenericC

A = GenericA # Just re-export the generic A class
B = GenericB # I'll subclass when I know what I want to do to it

#I really want to inherit from C and change it's behavior
class C(GenericC):
    def changeHello(self):
        self.hello = "Hello! I am Mr.C modified in t_Custom"

__all__ = ["A","B","C"]

关于自定义类的 Pythonic 方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912420/

相关文章:

python - base64.encodestring 在 python 3 中失败

单击按钮打开另一个窗口的 Python GUI 代码中断

python - 向右填充多行字符串

python - python 中的稀疏向量?

Python GTK 3+ 控制关闭窗口

python - BeautifulSoup img src 获取 base64 而不是实际链接

python-2.7 - 对于 python 中给定的值条件,将列名称作为数据框中的列表返回

python - 删除 python 字典中的一个级别,保留值

python - 使用 Python 请求访问经过身份验证的页面

python - 在python中使用日期获取周数