__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code)

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sat Jul 9 11:41:13 CEST 2005


My initial proposal
(http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't
exactly get a warm welcome...

And Now for Something Completely Different:

class autoinit(object):

  def __init__(self, *args, **keyword_args):
    self.__dict__.update(
      zip(self.__autoinit__.im_func.func_code.co_varnames[1:], args))
    self.__dict__.update(keyword_args)
    self.__autoinit__(*args, **keyword_args)

class grouping(autoinit):

  def __autoinit__(self, x, y, z):
    print self.x, self.y, self.z

group = grouping(1,2,z=3)
group = grouping(z=1,x=2,y=3)
try: grouping(1)
except TypeError, e: print e
try: grouping(1,2,3,a=0)
except TypeError, e: print e


Almost like my original favorite solution, only better, and it doesn't require
a syntax change.

Under a hypothetical new proposal __autoinit__ would become a standard feature
of object.

Any takers?

Cheers,
        Ralf


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Python-list mailing list