Python: override __init__ args in __new__
Posted
by EoghanM
on Stack Overflow
See other posts from Stack Overflow
or by EoghanM
Published on 2010-03-18T07:23:03Z
Indexed on
2010/03/18
7:41 UTC
Read the original article
Hit count: 373
I have a __new__ method as follows:
class MyClass(object):
def __new__(cls, *args):
new_args = []
args.sort()
prev = args.pop(0)
while args:
next = args.pop(0)
if prev.compare(next):
prev = prev.combine(next)
else:
new_args.append(prev)
prev = next
if some_check(prev):
return SomeOtherClass()
new_args.append(prev)
return super(MyClass, cls).__new__(cls, new_args)
def __init__(self, *args):
...
However, this fails with a deprecation warning:
DeprecationWarning: object.__new__() takes no parameters
SomeOtherClass can optionally get created as the args are processed, that's why they are being processed in __new__ and not in __init__
What is the best way to pass new_args to __init__?
Otherwise, I'll have to duplicate the processing of args in __init__ (without some_check)
© Stack Overflow or respective owner