idiom for constructor?

Chris Green cmgreen at uab.edu
Wed Jun 1 14:17:11 EDT 2005


"Mac" <idontneednostinkinid at yahoo.com> writes:

> Is there a nice Python idiom for constructors which would expedite the
> following?
>
> class Foo:
>   def __init__(self, a,b,c,d,...):
>     self.a = a
>     ...

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]

I don't think it saves you a whole lot of typing (and gains you a lot
of ugly complexity).  I find as soon as I do this, I then want to
manipulate a,b,c,d .

You might look to see if you can customize your editor to use
templates/interaction and then inserts the right text for you.
-- 
Chris Green <cmgreen at uab.edu>
"Yeah, but you're taking the universe out of context."



More information about the Python-list mailing list