Python:如何将列表变量的名称分配给类

2024-06-28 20:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我有一个列表,如下所示:

lst = [['B', 'A'], ['C', 'B'], ['D', 'B'], ['E','C D'] ......]

其中t[0]应该是t[1](它从中继承)的子类

我需要编写一个脚本,创建名称与lst匹配的空类,然后在t[i-1]t[i]的子类时进行检查:

if issubclass(B, A)== True:
   print('Yes')
   

到目前为止,我了解如何开始和完成脚本,但我完全不知道如何从列表中分配名称,以便可以使用issubclass。 也许还有其他方法可以创建类并跟踪它们的继承

我今天刚开始学习课程和OOP,所以在处理这个问题时,我很可能错过了一些关键的东西


Tags: 方法脚本名称true列表if子类oop
1条回答
网友
1楼 · 发布于 2024-06-28 20:17:37

我们定义类的通常方法自然是使用class关键字

class B(A):
    some_variable = 100

实际上,我们正在Python运行时中构建一个新类型。事实上,类型在Python中有自己的构造函数;它叫^{},我们可以直接叫它。上述声明大致相当于

B = type('B', ('A',), { 'some_variable': 100 })

现在类型名是字符串。我们还需要一块拼图。我们希望使用一个字符串将它赋给名称B,这样我们就可以使用事先不知道的名称。假设您希望在模块范围内执行此操作,我们可以使用^{},它返回当前模块顶级变量的字典,我们可以自由修改该字典以添加更多变量。所以我们可以

globals()['B'] = type('B', ('A',), { 'some_variable': 100 })

现在,让我们把这些部分放在一起,编写一个使用您建议的lst列表的脚本

lst = [['B', 'A'], ['C', 'B'], ['D', 'B'], ['E','C D']]

# Iterate over the list.
for class_name, superclass_names in lst:
    # Here, we're going to lookup all of the superclass names in the
    # current global scope. It's a little more complicated than that,
    # since we also want to *create* any names that don't exist (like
    # 'A' in your example) when we find them.
    superclasses = []
    for superclass_name in superclass_names.split(' '):
        # If the name doesn't exist, create it and assume its
        # supertype is object, the root of Python's type hierarchy.
        if superclass_name not in globals():
            globals()[superclass_name] = type(superclass_name, (object,), {})
        # Get the class, whether it's the one we just made or one that
        # already exists.
        superclasses.append(globals()[superclass_name])
    # Now we construct the new class. The first argument to type() is
    # the class name, the second is all of the superclasses (it must
    # be a tuple, not a list, according to the documentation, so we
    # convert it), and finally the contents. Since you just want the
    # classes themselves, I'll assume the contents are meant to be
    # empty. You can easily change that as needed.
    globals()[class_name] = type(class_name, tuple(superclasses), {})

# Now let's see that everything is defined correctly. __mro__ is a
# complicated concept, but the basic idea is that it should give us E,
# followed by all of its subclasses in a reasonable order (for some
# definition of reasonable).
print(E.__mro__)

Try it online!

相关问题 更多 >