[sapug] TypeError: __init__() got multiple values for keyword argument 'value'

Tim Wegener twegener at fastmail.fm
Tue Apr 29 06:32:42 CEST 2008


On Tue, 2008-04-29 at 13:19 +0930, Darryl Ross wrote:
> Heya,
> 
> I'm sure I've worked out how to to this in the past, but the syntax is 
> escaping me right now.
> 
> 
> class DooHicky(object):
>      def __init__(self, arg1=None, arg2=None):
>        pass
> 
> class Thingo(DooHicky):
>      def __init__(self, value=None, *args, **kwargs):
>          self.value = value
>          super(Thingo, self).__init__(self, *args, **kwargs)
> 
> 
> Basically, I want to pass in an argument to my "Thingo" class which does 
> not get passed back to the parent constructor.
> 
> Any ideas / pointers?

# If you expect the value argument to be unspecified more often than not:
class Thingo(DooHicky):
    def __init__(self, *args, **kwargs):
        self.value = kwargs.get('value')
	if self.value is not None:
		del kwargs['value']
        super(Thingo, self).__init__(self, *args, **kwargs)

# If you expect the value argument to be specified more often than not:
class Thingo(DooHicky):
    def __init__(self, *args, **kwargs):
	try:
            self.value = kwargs.pop('value')
	except KeyError:
	    pass
        super(Thingo, self).__init__(self, *args, **kwargs)

Both of these will require you to pass value as a keyword argument. 

You might also want to consider avoiding the use of inheritance. I
can't really comment on whether it is desirable to use inheritance
here, given the supplied information. Personally I try to avoid
inheritance unless it is a really nice fit for the situation.

Tim





More information about the sapug mailing list