Answer the question
In order to leave comments, you need to log in
How to override metaclass's __new__ signature to add default values?
class Meta(type):
def __new__(mcs, name, bases, dct=None):
if not dct:
dct = {}
return super().__new__(mcs, name, bases, dct)
def __init__(self, name, bases, dct):
pass
Meta('NewClassName', tuple())
type.__new__() takes exactly 3 arguments (2 given)
class Meta(type):
def __new__(mcs, name, bases, *args, **kwargs):
if not args and not kwargs:
args = (dict(),)
return super().__new__(mcs, name, bases, *args)
def __init__(self, name, bases, *args, **kwargs):
pass
Meta('NewClassName', tuple())
Answer the question
In order to leave comments, you need to log in
class Meta(type):
@classmethod
def __prepare__(metacls, name, bases):
...
def __new__(mcs, name, bases, dct=None):
if not dct:
dct = {}
return super().__new__(mcs, name, bases, dct)
Meta('NewClassName', tuple())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question