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

Viewed 68101

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!

4 Answers

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

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

Related