the right way of handling **args for __init__ and other methods

Oivvio Polite oivvioREMOVETHIS at cajal.mbb.ki.se
Mon Jul 24 10:57:36 EDT 2000


Hi all.
I guess this is more of a design question than a howto question.

It has to do with implementing arbitrary-argument set methods.
I want the user to be able to use the same interface with __init__ and read.
This is a stripped down version:
class argeater:
    def __init__(self, somearg, **args):
        print "init ",args
        self.data = {}
        self.read(somearg, argdict=args)

    def read(self, somearg, argdict=None,**args):
        print "read ",args
        print "argdict ",argdict

        #putting everything from argdict into args
        #feels a bit awkward.
        if argdict:
            assert type(argdict) == types.DictionaryType
            for key in argdict.keys():
                if not args.has_key(key):
                    args[key] = argdict[key]

        #now do a lot of stuff to self.data
        #based on contents of args

#but the user will have a nice and uniform interface to the class.
spam = argeater(1, a = 1, b = 2,c = 3)
spam.read(1, a = 4, b = 5,c = 6)

is this have you would have done it?
oivvio





More information about the Python-list mailing list