Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating a metaclass in python

I wrote a class in python which inherits from type . I thought that this was the only requirement for a class so as to be called as a metaclass but had not defined a __new__ method for it. But on instantiating with this new class as the metaclass I got an error stating the below :

TypeError: type.__new__() takes exactly 3 arguments (0 given)

The following is my code :

class a(type) :
    pass 
c= a()

Now when the class statement is being processed , that the __new__ method of type is being called is my assumption. This is because the default metaclass of all classes in python is type .

Now when I am instantiating the class a , which I have assumed to be a metaclass under the assumption that any class inheriting from (type) is a metaclass , isn't it the same as creating a class ? Why should this not result in type.__new__ being called with correct arguments ?

like image 274
itp dusra Avatar asked Apr 19 '26 15:04

itp dusra


2 Answers

This does not work:

class a(type) :
    pass 
c = a()

...for the same reason for which this does not work:

c = type()

In the end, both do the same.

To use it as a metaclass, do this:

>>> class Class(metaclass=a):
...     pass
...
>>> Class
<class '__main__.Class'>
>>> type(Class)
<class '__main__.a'>

You could also instantiate the class directly, as you tried, but you have to provide the correct arguments:

AnotherClass = type('AnotherClass', (), {})

YetAnotherClass = a('YetAnotherClass', (), {})
like image 158
zvone Avatar answered Apr 21 '26 05:04

zvone


This error is due to you not respecting type's signature.

Inheriting from type is indeed enough for a class to be used as a metaclass, but the thing is you actually have to use it as a metaclass.

type itself has "two working modes: if called with 3 positional arguments, it creates a new class. And then type is the metaclass of that class. If called with 1 positional argument, it creates no new class or object at all - instead, it just returns that object's class.

But it makes no sense calling type with no arguments at all. And the arguments in the modes above are not optional. So, you will get a TypeError if your try to call type with no arguments at all - and that is not a "TypeError because something went wrong with the type class" - it is a "TypeError because your call did not match the callable signature".

When you inherit from type and change nothing, you class will behave exactly the same as the original type: you can call it with either one or three positional arguments, and the code responsible for working in either mode lies in type.__new__.

Now, if you want to use your class as a metaclass, you can indeed call it, but in the three argument form: you ass it the new class name, its bases and its attributes - which can actually be all empty, but you have to pass a string, a tuple and a dictionary as these three arguments:

class A(type): pass

myclass = A("", (), {})

And now, A is working as the metaclass for myclass:

In [16]: type(myclass)                                                                                   
Out[16]: __main__.A

However, whenever one defines a metaclass it is more usual to use it with the metaclass= named argument when declaring a class body:

In [17]: class  MyOtherClass(metaclass=A): 
    ...:     pass 
    ...:                                                                                                 

In [18]: type(MyOtherClass)                                                                              
Out[18]: __main__.A

Python's runtime will then compile this class body, and when the bytecod for it is executed, it will make the call to your metaclass' __new__ (and then __init__, and before that its __prepare__) method, so that it works as a metaclass.

So, just in case it is not clear: when you derive a class from type intending to use it as a metaclass, there is no need to further instantiate it to say that "it is now a metaclass". A subclass of type already can be a metaclass, and its instances will be classes, that will have it as a metaclass.

like image 31
jsbueno Avatar answered Apr 21 '26 03:04

jsbueno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!