Skip to main content
 首页 » 编程设计

自定义类的pythonic方式

2024年10月25日12xiaohuochai

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

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" 

问题:做这样的事情的 pythonic 方法是什么?我应该像选项 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"]