我设置了两个类(还有一些其他不相关的属性)。你知道吗
class Alcohol():
def __init__(FunctionalGroup):
FunctionalGroup.Naming = ["hydroxy", "ol"]
class Halogenoalkane():
def __init__(FunctionalGroup):
FunctionalGroup.Naming = ["chloro", "bromo", "iodo"]
我希望能够将一个给定的字符串(如ethanol
或2-chloromethane
)排序到其中一个字符串中,并创建一个实例,该实例基于名称适合哪个类。例如:
>>> Name: Ethanol
This is an alcohol.
我正在寻找一种方法来迭代每个类中的FunctionalGroup.Naming
列表,并检查字符串中是否包含它们中的任何一个。你知道吗
做这个或其他数据结构的最佳方法是什么?你知道吗
(抱歉,如果你不喜欢化学,我只是想让复习更有趣)
我不确定这是否是最干净的方法,我删除了实例变量,并在每个类中创建了一个常量列表。这样比较容易引用,而且列表似乎是一个常量:
输出:
相关问题 更多 >
编程相关推荐