Two notable ways to create a class are shown below:
class Klass:
pass
Klass = type("Klass", tuple(), dict())
I would like to override the constructor (__call__
) while still using the class
keyword instead of doing something else, like directly calling type
. I really do want to override (__call__
), not __init__
My failed attempts are shown below:
Attempt 1
class Foo:
@classmethod
def __call__(*args):
print("arr har")
return super(type(args[0]), args[0]).__call__(*args)
instance = Foo()
# did not print "arr har"
Attempt 2
class BarMeta(type):
def __call__(*args):
print("hello world")
return super(type(args[0]), args[0]).__call__(*args[1:])
Attempt 2A
class Bar: __metaclass__ = BarMeta instance = Bar() # did not print "hello world"
Attempt 2B
Baz = BarMeta("Baz", tuple(), dict()) instance = Baz() # Did print "hello world," but we weren't able to use the `class` keyword to create `Baz`