what exactly does type.__call__ do?

Jason Maldonis jjmaldonis at gmail.com
Wed Nov 1 23:11:54 EDT 2017


Ok no worries then! Thanks for the tips. I might wait until tomorrow then
until someone comes along who deals with metaclasses and alternate class
constructors.

In case you're curious, I'm doing two things that are relevant here, and
I'll link the python3 cookbook examples that are super useful (I love that
book):
http://chimera.labs.oreilly.com/books/1230000000393/ch09.
html#_discussion_155
and
http://chimera.labs.oreilly.com/books/1230000000393/ch08.html#_solution_134

They explain things very well. Metaclasses are so useful when you hit a use
case that they fit into. I don't often deal with multiple class
constructors, so I'm a bit new to that territory and I'm trying to delve
into the details of how python classes are constructed (namely, whether
`type.__call__` does more than just call `cls.__new__` and `self.__init__`).

Thanks for the help too.

On Wed, Nov 1, 2017 at 8:49 PM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:

> ram at zedat.fu-berlin.de (Stefan Ram) writes:
> >|PyObject *meth = lookup_method(self, &PyId___call__);
> ...
> >|Call self as a function.
>
>   Oh, I guess this might be what makes
>
> int(x)
>
>   call
>
> int.__call__(x)
>
>   . And all other instances of this metaclass.
>
>   Let's try to overwrite it to call 'charles' instead.
>
>   main.py
>
> class mymetaclass( type ):
>     def __call__( this, *args ):
>         this.charles( *args )
>
> class myclass( metaclass=mymetaclass ):
>     def charles():
>         print( "Charles" )
>
> myclass()
>
>   transcript
>
> Charles
>
>   Ok, while I managed to make »myclass« print »Charles«
>   using a »__call__« function of it's metaclass, I still
>   don't know what I am doing. This is the first time I
>   actually deal with the topic of metaclasses. I have not
>   yet read a text book chapter about them, so I'm still
>   in the dark like the Chinese in the Chinese room.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list