idiom for constructor?

Laszlo Zsolt Nagy gandalf at geochemsource.com
Wed Jun 1 16:11:18 EDT 2005


>You could try:
>
>class Foo:
>   def __init__(self,a,b,c,d):
>       args = locals()
>       for arg in args.keys():
>           if name!='self':
>              self.__dict__[arg] = args[arg]
>  
>
Should be:

if arg!='self'

Also it is not perfect. For example:

class Foo:
    def __init__(self,a,b,c,d):
        temp = 12
        self.foo2 = temp + 4
        args = locals()
        for arg in args.keys():
            if arg!='self':
                self.__dict__[arg] = args[arg]
   
a = Foo(1,2,3,4)
print dir(a)

Results in:

['__doc__', '__init__', '__module__', 'a', 'b', 'c', 'd', 'foo2', 'temp']

E.g. not only the parameters but all local variables are used...



More information about the Python-list mailing list