what exactly does type.__call__ do?
Jason Maldonis
jjmaldonis at gmail.com
Wed Nov 1 19:13:29 EDT 2017
Hi everyone,
I want to use a metaclass to override how class instantiation works. I've
done something analogous to using the Singleton metaclass from the Python3
Cookbook example.
However, I want to provide a classmethod that allows for "normal" class
instantiation that prevents this metaclass from being used.
To do that, I think I just make a @classmethod constructor function.
However, I can imagine a few different ways of writing this:
@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
Is one of these correct? Or do they all do the same thing?
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.
More information about the Python-list
mailing list