Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Metaclass of a metaclass

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


steve+comp.lang.python at pearwood

Jun 5, 2012, 1:48 AM

Post #1 of 4 (213 views)
Permalink
Metaclass of a metaclass

I was playing around with metaclasses and I wondered what would happen if
the metaclass itself had a metaclass. Sort of a metametaclass.

Apparently it gives an error. Can anyone explain why this does not work?

# Python 3.2



>>> class MyType(type): # A metaclass...
... def __repr__(self):
... s = super().__repr__()
... return s.replace('class', 'metaclass')
...
>>> class Meta(metaclass=MyType): # ... of a metaclass.
... pass
...
>>> Meta
<metaclass '__main__.Meta'>
>>>
>>> isinstance(Meta, type)
True
>>>
>>>
>>> class MyClass(metaclass=Meta): # And now try to use it.
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object.__new__() takes no parameters



What am I doing wrong?


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Jun 5, 2012, 2:32 AM

Post #2 of 4 (198 views)
Permalink
Re: Metaclass of a metaclass [In reply to]

Steven D'Aprano wrote:

> I was playing around with metaclasses and I wondered what would happen if
> the metaclass itself had a metaclass. Sort of a metametaclass.
>
> Apparently it gives an error. Can anyone explain why this does not work?
>
> # Python 3.2
>
>
>
>>>> class MyType(type): # A metaclass...
> ... def __repr__(self):
> ... s = super().__repr__()
> ... return s.replace('class', 'metaclass')
> ...
>>>> class Meta(metaclass=MyType): # ... of a metaclass.
> ... pass
> ...
>>>> Meta
> <metaclass '__main__.Meta'>
>>>>
>>>> isinstance(Meta, type)
> True

I think you want isinstance(Meta(), type), and this returns False.

>>>> class MyClass(metaclass=Meta): # And now try to use it.
> ... pass
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: object.__new__() takes no parameters
>
>
>
> What am I doing wrong?

class A(metaclass=M):
pass

is equivalent to

A = M(name, bases, classdict)

and as the error message suggests B needs a compatible signature.


--
http://mail.python.org/mailman/listinfo/python-list


ian.g.kelly at gmail

Jun 5, 2012, 3:39 AM

Post #3 of 4 (199 views)
Permalink
Re: Metaclass of a metaclass [In reply to]

On Tue, Jun 5, 2012 at 2:48 AM, Steven D'Aprano
<steve+comp.lang.python [at] pearwood> wrote:
> I was playing around with metaclasses and I wondered what would happen if
> the metaclass itself had a metaclass. Sort of a metametaclass.
>
> Apparently it gives an error. Can anyone explain why this does not work?

In your example, Meta is not actually a metaclass, as it inherits from
object, not type. Here's an example that works:

>>> class MetaMeta(type): pass
...
>>> class Meta(type, metaclass=MetaMeta): pass
...
>>> class MyClass(metaclass=Meta): pass
...

Anyway, metaclasses of metaclasses is not that unusual, as type is
already its own metaclass.

Cheers,
Ian
--
http://mail.python.org/mailman/listinfo/python-list


bruno.desthuilliers at gmail

Jun 5, 2012, 8:30 AM

Post #4 of 4 (197 views)
Permalink
Re: Metaclass of a metaclass [In reply to]

On Jun 5, 10:48 am, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> Apparently it gives an error. Can anyone explain why this does not work?
>
> # Python 3.2
>
> >>> class MyType(type):  # A metaclass...
>
> ...     def __repr__(self):
> ...             s = super().__repr__()
> ...             return s.replace('class', 'metaclass')
>
> >>> class Meta(metaclass=MyType):  # ... of a metaclass.
>
> ...     pass

(...)

> >>> class MyClass(metaclass=Meta):  # And now try to use it.
>
> ...     pass
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: object.__new__() takes no parameters
>
> What am I doing wrong?

Meta inherit from object, but being used as a metaclass, Meta.__new__
is called with name, bases, dict etc as arguments.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.