user232137
user232137

Reputation: 23

why class creation throws error

Just playin around class

def func(*args, **kwargs):
    print args, kwargs

class Klass(func): pass

it throws error

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

What does it mean, i am passing no str nowhere? and yes I should be passing class in bases but why didn't error say that, instead of this cryptic error?

Upvotes: 2

Views: 2585

Answers (1)

Anurag Uniyal
Anurag Uniyal

Reputation: 88727

see here for the reason for the cryptic msg

http://bugs.python.org/issue6829

questions Error when calling the metaclass bases: function() argument 1 must be code, not str has same problem.

Edit: play-around

Though you can use metaclass to make it work in a twisted way ;)

def func(name, klassDict):
    return type(name, (), klassDict)

class MyMeta(type):
    def __new__(self, name, bases, klassDict):
        return bases[0](name, klassDict)

class Klass(func):
    __metaclass__ = MyMeta

print Klass

Upvotes: 2

Related Questions