【问题标题】:Python Error :Inheriting 'X', which is not a classPython错误:继承'X',这不是一个类
【发布时间】:2021-07-19 11:08:11
【问题描述】:

我已经使用 Visual Studio 代码编写了代码

from datetime import date

Title = 'Actividad #$'
Complexity = 0
Time = 5 #cantidad en dias

class Activitie(Title,Complexity,Time):
    """Log your activity with this class"""

    Title = 'Actividad #$'
    Complexity = 0
    Time = 5 #cantidad en dias

它显示

Inheriting 'Time', which is not a class.
Inheriting 'Complexity', which is not a class.
Inheriting 'Title', which is not a class.

和...

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

感谢您的任何解决方案

【问题讨论】:

  • 标题是字符串,复杂度和时间都是整数。当你把它们放在你的类声明中时,python 期望它们是你正在继承的类,但它们不是。因此你得到一个错误。 90% 的时间错误消息非常清楚地显示了问题,但人们没有注意
  • 错误告诉你确切的问题,TitleComplexityTime不是类对象,所以你不能从它们继承类定义语句。我不知道你打算做什么。我建议reading the official documentation/tutorial
  • 我不明白你希望class Activitie(Title,Complexity,Time) 做什么。您是否希望实现__init__ 方法?我认为你需要更多地关注你用来学习 Python 的东西; Stack Overflow 不是学习基础知识的好地方。

标签: python oop inheritance pylint


【解决方案1】:

看起来你想定义类对象构造函数,但你不小心使用了 Python 的类继承语法。

下面是定义类及其对象构造函数的方法:

class Activitie:
    def __init__(self, Title, Complexity, Time):
    """Log your activity with this class"""

        self.Title = Title
        self.Complexity = Complexity
        self.Time = Time 

您收到此继承错误是因为在 Python 中声明继承的语法如下所示:

SubClass(ParentClassA, ParentClassB, ParentClassC):

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多