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

发布于 2024-08-21 05:45:10 字数 432 浏览 2 评论 0原文

我今天早些时候尝试对 threading.Condition 进行子类化,但没有成功。这是当我尝试子类化 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

有人可以解释这个错误吗?谢谢!

I tried to subclass threading.Condition earlier today but it didn't work out. Here is the output of the Python interpreter when I try to subclass the threading.Condition class:

>>> 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨轻弹 2024-08-28 05:45:10

您会收到该异常,因为尽管它的名称类似于类,但 threading.Condition 是一个函数,并且您不能对函数进行子类化。

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

这个不太有用的错误消息已在 Python bugtracker 上提出,但已被标记为“won不修。”

You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions.

>>> 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."

傻比既视感 2024-08-28 05:45:10

与 OP 遇到的问题不同,但如果您尝试从模块而不是类进行子类化(例如,您尝试继承 My.Module 而不是 My.Module.Class),您也可能会收到此错误。感谢这篇文章帮助我解决了这个问题。

TypeError:调用元类基时出错

对于这个,答案是您可能将一个 python 类命名为
与它所在的模块(即文件)相同。然后
导入该模块并尝试像类一样使用它。你做了这个
因为你和我一样,成为 Java 程序员的时间可能不长
以前:-)。修复它的方法是导入 module.class 而不是
只是模块。或者,为了理智起见,更改类的名称
或模块,以便更明显地显示正在导入的内容。

Different problem than OP had, but you can also get this error if you try to subclass from a module instead of a class (e.g. you try to inherit My.Module instead of My.Module.Class). Kudos to this post for helping me figure this out.

TypeError: Error when calling the metaclass bases

For this one, the answer is that you probably named a python class the
same thing as the module (i.e., the file) that it's in. You then
imported the module and attempted to use it like a class. You did this
because you, like me, were probably a Java programmer not that long
ago :-). The way to fix it is to import the module.class instead of
just the module. Or, for sanity's sake, change the name of the class
or the module so that it's more obvious what's being imported.

落日海湾 2024-08-28 05:45:10

关于子类化模块,如果您在文件 Foo.py 中定义了类 Foo,那么这是一个非常容易犯的错误。

当您在不同的文件中创建 Foo 的子类时,您可能会意外地执行以下操作(这是对模块进行子类化的尝试,并将导致错误):

import Foo
class SubclassOfFoo(Foo):

当您确实需要执行以下任一操作时:

from Foo import Foo
class SubclassOfFoo(Foo):

或:

import Foo
class SubclassofFoo(Foo.Foo):

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.

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):

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):
通知家属抬走 2024-08-28 05:45:10

遇到了同样的问题。最终通过仔细查看代码解决了这个问题,这就是关于字符串而不是代码发出警报的 TypeError 出现的地方。

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

“并且”

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

请注意,由于单个小写字母而出现特定错误到代码“Model”,这又使其成为一个字符串

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..

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文