Python New-style Classes and the Super Function

Posted by sfjedi on Stack Overflow See other posts from Stack Overflow or by sfjedi
Published on 2010-05-04T06:47:07Z Indexed on 2010/05/04 6:58 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

This is not the result I expect to see:

class A(dict):
    def __init__(self, *args, **kwargs):
        self['args'] = args
        self['kwargs'] = kwargs

class B(A):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(args, kwargs)

print 'Instance A:', A('monkey', banana=True)
#Instance A: {'args': ('monkey',), 'kwargs': {'banana': True}}

print 'Instance B:', B('monkey', banana=True)
#Instance B: {'args': (('monkey',), {'banana': True}), 'kwargs': {}}

I'm just trying to get classes A and B to have consistent values set. I'm not sure why the kwargs are being inserted into the args, but I'm to presume I am either calling init() wrong from the subclass or I'm trying to do something that you just can't do.

Any tips?

© Stack Overflow or respective owner

Related posts about python

Related posts about super