W
wzab
I had to implement in Python 2.7.x a system which heavily relies on
multiple inheritance.
Working on that, I have came to very simplistic code which isolates
the problem:
(The essential thing is that each base class receives all arguments
and uses only those,
which it understands).
class a(object):
def __init__(self,*args,**kwargs):
super(a,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in a"
class b(object):
def __init__(self,*args,**kwargs):
super(b,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in b"
class c(a,b):
def __init__(self,*args,**kwargs):
super(c,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in c"
z=c(test=23,data="eee")
In Python 2.5.2 the above code works correctly, and produces:
$python test1.py
()
{'test': 23, 'data': 'eee'}
init in b
()
{'test': 23, 'data': 'eee'}
init in a
()
{'test': 23, 'data': 'eee'}
init in c
Unfortunately in Python 2.7 the above code generates an exception:
$ python test1.py
Traceback (most recent call last):
File "test1.py", line 22, in <module>
z=c(test=23,data="eee")
File "test1.py", line 17, in __init__
super(c,self).__init__(*args,**kwargs)
File "test1.py", line 3, in __init__
super(a,self).__init__(*args,**kwargs)
File "test1.py", line 10, in __init__
super(b,self).__init__(*args,**kwargs)
TypeError: object.__init__() takes no parameters
I have found a workaround:
# Class my_object added only as workaround for a problem with
# object.__init__() not accepting any arguments.
class my_object(object):
def __init__(self,*args,**kwargs):
super(my_object,self).__init__()
class a(my_object):
def __init__(self,*args,**kwargs):
super(a,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in a"
class b(my_object):
def __init__(self,*args,**kwargs):
super(b,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in b"
class c(a,b):
def __init__(self,*args,**kwargs):
super(c,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in c"
z=c(test=23,data="eee")
The above works correctly, producing the same results as the first
code in Python 2.5.2,
but anyway it seems to me just a dirty trick...
What is the proper way to solve that problem in Python 2.7.3?
multiple inheritance.
Working on that, I have came to very simplistic code which isolates
the problem:
(The essential thing is that each base class receives all arguments
and uses only those,
which it understands).
class a(object):
def __init__(self,*args,**kwargs):
super(a,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in a"
class b(object):
def __init__(self,*args,**kwargs):
super(b,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in b"
class c(a,b):
def __init__(self,*args,**kwargs):
super(c,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in c"
z=c(test=23,data="eee")
In Python 2.5.2 the above code works correctly, and produces:
$python test1.py
()
{'test': 23, 'data': 'eee'}
init in b
()
{'test': 23, 'data': 'eee'}
init in a
()
{'test': 23, 'data': 'eee'}
init in c
Unfortunately in Python 2.7 the above code generates an exception:
$ python test1.py
Traceback (most recent call last):
File "test1.py", line 22, in <module>
z=c(test=23,data="eee")
File "test1.py", line 17, in __init__
super(c,self).__init__(*args,**kwargs)
File "test1.py", line 3, in __init__
super(a,self).__init__(*args,**kwargs)
File "test1.py", line 10, in __init__
super(b,self).__init__(*args,**kwargs)
TypeError: object.__init__() takes no parameters
I have found a workaround:
# Class my_object added only as workaround for a problem with
# object.__init__() not accepting any arguments.
class my_object(object):
def __init__(self,*args,**kwargs):
super(my_object,self).__init__()
class a(my_object):
def __init__(self,*args,**kwargs):
super(a,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in a"
class b(my_object):
def __init__(self,*args,**kwargs):
super(b,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in b"
class c(a,b):
def __init__(self,*args,**kwargs):
super(c,self).__init__(*args,**kwargs)
print args
print kwargs
print "init in c"
z=c(test=23,data="eee")
The above works correctly, producing the same results as the first
code in Python 2.5.2,
but anyway it seems to me just a dirty trick...
What is the proper way to solve that problem in Python 2.7.3?