动态选择要继承的类 [英] Dynamically choosing class to inherit from
本文介绍了动态选择要继承的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Python知识有限,在以下情况下我需要一些帮助。
My Python knowledge is limited, I need some help on the following situation.
假设我有两个类, A
和 B
,是否有可能在Python中(概念上)执行以下操作:
Assume that I have two classes A
and B
, is it possible to do something like the following (conceptually) in Python:
import os
if os.name == 'nt':
class newClass(A):
# class body
else:
class newClass(B):
# class body
所以问题是我会想要创建一个类 newClass
,使其将基于平台差异从不同的基类继承,这可以在Python中做到吗?
谢谢。
So the problem is that I would like to create a class newClass
such that it will inherit from different base classes based on platform difference, is this possible to do in Python?
Thanks.
推荐答案
您可以使用条件表达式:
class newClass(A if os.name == 'nt' else B):
...
这篇关于动态选择要继承的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文