简体   繁体   English

调用元类基时出错:function() 参数 1 必须是代码,而不是 str

[英]Error when calling the metaclass bases: function() argument 1 must be code, not str

I tried to subclass threading.Condition earlier today but it didn't work out.我今天早些时候尝试将 threading.Condition 子类化,但没有成功。 Here is the output of the Python interpreter when I try to subclass the threading.Condition class:这是当我尝试子类化 threading.Condition 类时 Python 解释器的输出:

>>> import threading
>>> class ThisWontWork(threading.Condition):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str

Can someone explain this error?有人可以解释这个错误吗? Thanks!谢谢!

You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions.您会收到该异常,因为尽管它的名称类似于类,但threading.Condition是一个函数,并且您不能对函数进行子类化。

>>> type(threading.Condition)
<type 'function'>

This not-very-helpful error message has been raised on the Python bugtracker , but it has been marked "won't fix."这个不是很有帮助的错误消息已经在 Python bugtracker 上提出,但它被标记为“不会修复”。

Different problem than OP had, but you can also get this error if you try to subclass from a module instead of a class (eg you try to inherit My.Module instead of My.Module.Class).与 OP 的问题不同,但如果您尝试从模块而不是类进行子类化(例如,您尝试继承 My.Module 而不是 My.Module.Class),您也会收到此错误。 Kudos to this post for helping me figure this out.感谢 这篇文章帮助我解决了这个问题。

TypeError: Error when calling the metaclass bases TypeError:调用元类基类时出错

For this one, the answer is that you probably named a python class the same thing as the module (ie, the file) that it's in. You then imported the module and attempted to use it like a class.对于这个问题,答案是您可能将 Python 类命名为与其所在的模块(即文件)相同的名称。然后您导入了该模块并尝试像使用类一样使用它。 You did this because you, like me, were probably a Java programmer not that long ago :-).你这样做是因为你和我一样,不久前可能还是一名 Java 程序员:-)。 The way to fix it is to import the module.class instead of just the module.修复它的方法是导入 module.class 而不仅仅是模块。 Or, for sanity's sake, change the name of the class or the module so that it's more obvious what's being imported.或者,为了理智起见,更改类或模块的名称,以便更清楚地了解要导入的内容。

With respect to subclassing a module, this is a really easy mistake to make if you have, for example, class Foo defined in a file Foo.py.关于对模块进行子类化,例如,如果您在文件 Foo.py 中定义了类 Foo,则这是一个非常容易犯的错误。

When you create a subclass of Foo in a different file, you might accidentally do the following (this is an attempt to subclass a module and will result in an error):当您在不同的文件中创建 Foo 的子类时,您可能会不小心执行以下操作(这是对模块进行子类化的尝试,将导致错误):

import Foo
class SubclassOfFoo(Foo):

when you really need to do either:当您确实需要执行以下任一操作时:

from Foo import Foo
class SubclassOfFoo(Foo):

or:或者:

import Foo
class SubclassofFoo(Foo.Foo):

Gotten into the same problem.陷入了同样的问题。 Finally solved by taking a keen look at the code and this is where the TypeError that alarms about a string instead of code comes about..最后通过仔细查看代码解决了,这就是TypeError警告字符串而不是代码的地方。

Class Class_name(models.model): //(gives a TypeError of 'str' type) 

"And" “和”

Class Class_name(models.Model): // is the correct one. 

Notice that specific error comes about because of a single lowercase letter to the code " Model " which in turn makes it a string请注意,特定错误是由于代码“ Model ”的单个小写字母而导致的,这反过来又使它成为一个字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM