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

David Underhill picture David Underhill · Feb 9, 2010 · Viewed 53.9k times · Source

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!

Answer

Will McCutchen picture Will McCutchen · Feb 9, 2010

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