Thanks for the reply. And I think I wasn't clear enough. I was wondering what the metaclass `type`'s `type.__call__` does explicitly. I'm reasonably comfortable writing metaclasses when I need them, and I understand how `.__call__` works for non-metaclass objects.
In my first email I gave three possible ways to write a @classmethod that returns an instance of that class. I'm not sure which one is the "most pythonic" or "most correct", but I'm pretty sure I can get them all to work. I'll paste them below again, and I'm wondering which one I should use and why. Thanks! @classmethod def normal_constructor(cls, *args, **kwargs): return type.__call__(*args, **kwargs) @classmethod def normal_constructor(cls, *args, **kwargs): return super(???).__call__(*args, **kwargs) # I'm not sure what should go in the super here (I'm using python3) @classmethod def normal_constructor(cls, *args, **kwargs): self = cls.__new__(cls) self.__init__(*args, **kwargs) return self On Wed, Nov 1, 2017 at 6:51 PM, Stefan Ram <r...@zedat.fu-berlin.de> wrote: > Jason Maldonis <jjmaldo...@gmail.com> writes: > >I was looking for documentation for what exactly `type.__call__` does so > >that I can emulate it, but I wasn't able to find any docs explicitly > >detailing what that method does. If someone knows where this info is that > >would be great too. > > Instances are callable if their class has a __call__ method. > > When an instance is called, this __call__ method will be called. > > main.py > > class example: > def __call__( this ): > print( 'called' ) > instance = example() > instance() > > transcript > > called > > The general contract of this method is that it can do > whatever the author of the class deems appropriate. > > What exectly this is has to be documented in the > documentation of each specific class. > > In fact, the attribute »instance.__call__« has a »__call__« > attribute itself, thus: > > main.py > > class example: > def __call__( this ): > print( 'called' ) > instance = example() > instance() > instance.__call__() > instance.__call__.__call__() > > transcript > > called > called > called > > A type also is an instance, so > > int.__call__(whatever) > > should do the same as > > int( whatever ) > > . In Python 2 they had > > operator.isCallable( obj ) > > , in Python 3 we can emulate this using > > hasattr( obj, '__call__' ) > > . > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list