Sunday, May 01, 2005

ASPN : Python Cookbook : Automatically set all passed parameters to self

ASPN : Python Cookbook has a recipe for automatically assigning variables passed in to the equivalent self.variable in the class.

The simplest way is found in the comments:
Instead of specifying it as:
class Old:
def __init__(self, a, b, c, d, e, f, g, h, i=200, j=100):
self.a = a
self.b = b
self.c = c
self.d = d
... and so on

do it as:

class FooBar:
def __init__(self, a,b,c,d,e,f,g,h, i=100, j=100):
self.__dict__.update(locals())

0 Comments:

Post a Comment

<< Home