what is the idiom for copy lots of params into self?

E

Emin

Dear Experts,

When writing large classes, I sometimes find myself needing to copy a
lot of parameters from the argument of __init__ into self. Instead of
having twenty lines that all basically say something like self.x = x, I
often use __dict__ via something like:

class example:
def __init__(self,a,b,c,d,e,f,g,h,i,j,k,l,m,n):
for name in
['a','b','c','d','e','f','g','h','i','j','k','l','m','n']:
self.__dict__[name] = locals()[name]

This saves a lot of code and makes it easier to see what is going on,
but it seems like there should be a better idiom for this task. Any
suggestions?

Thanks,
-Emin
 
S

Steve Holden

Jean-Paul Calderone said:
Dear Experts,

When writing large classes, I sometimes find myself needing to copy a
lot of parameters from the argument of __init__ into self. Instead of
having twenty lines that all basically say something like self.x = x, I
often use __dict__ via something like:

class example:
def __init__(self,a,b,c,d,e,f,g,h,i,j,k,l,m,n):
for name in
['a','b','c','d','e','f','g','h','i','j','k','l','m','n']:
self.__dict__[name] = locals()[name]

This saves a lot of code and makes it easier to see what is going on,
but it seems like there should be a better idiom for this task. Any
suggestions?

I use a helper, like

http://divmod.org/trac/browser/trunk/Epsilon/epsilon/structlike.py#L35
If you don't want to go that far then this might give you an idea or two:
... def __init__(self,a,b,c,d,e,f,g,h,i,j,k,l,m,n):
... for name in inspect.getargspec(example.__init__)[0]:
... print name
...
self
a
b
c
d
e
f
g
h
i
j
k
l
m
n

regards
Steve
 
B

bearophileHUGS

Emin:
This saves a lot of code and makes it easier to see what is going on,
but it seems like there should be a better idiom for this task. Any
suggestions?

I know two ways of doing it, one way requires very light code into the
__init__ and it uses a decorator that takes the parameters and updates
self, but I have seen it's not much reliable in the implementation I
have.
The other way requires a bit more code into the method, but the
function code is quite short, easy to understand, and it seems reliable
enough (code from web.py, a bit modified):

def selfassign(self, locals):
for key, value in locals.iteritems():
if key != 'self':
setattr(self, key, value)

Used on the first line as:

def __init__(self, foo, bar, baz=1):
selfassign(self, locals())

Bye,
bearophile
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

Emin said:
Dear Experts,

When writing large classes, I sometimes find myself needing to copy a
lot of parameters from the argument of __init__ into self. Instead of
having twenty lines that all basically say something like self.x = x, I
often use __dict__ via something like:

class example:
def __init__(self,a,b,c,d,e,f,g,h,i,j,k,l,m,n):
for name in
['a','b','c','d','e','f','g','h','i','j','k','l','m','n']:
self.__dict__[name] = locals()[name]

This saves a lot of code and makes it easier to see what is going on,
but it seems like there should be a better idiom for this task. Any
suggestions?

Thanks,
-Emin


How about using variable length argumens?

class example:
def __init__(self, *args):
for i in args:
self.__dict__ = i

x = example('uno','dos','tres')

Luis
 
E

Emin

Dear Luis and everyone else who responded,

Thanks for your suggestions. One issue with using *args or **kw is that
I might no want to copy all the arguments to __init__ into self.

What made me ask the question in my original post was not so much that
I had to loop over the names I wanted to save, but whether it's okay to
mess with self.__dict__ or if there is another way I should be
assigning to self.

Thanks,
-Emin

Emin said:
Dear Experts,
When writing large classes, I sometimes find myself needing to copy a
lot of parameters from the argument of __init__ into self. Instead of
having twenty lines that all basically say something like self.x = x, I
often use __dict__ via something like:
class example:
def __init__(self,a,b,c,d,e,f,g,h,i,j,k,l,m,n):
for name in
['a','b','c','d','e','f','g','h','i','j','k','l','m','n']:
self.__dict__[name] = locals()[name]
This saves a lot of code and makes it easier to see what is going on,
but it seems like there should be a better idiom for this task. Any
suggestions?
Thanks,
-EminHow about using variable length argumens?

class example:
def __init__(self, *args):
for i in args:
self.__dict__ = i

x = example('uno','dos','tres')

Luis
 
B

Bjoern Schliessmann

Emin said:
Thanks for your suggestions. One issue with using *args or **kw is
that I might no want to copy all the arguments to __init__ into
self.

Try prepending something like

allowedParms = ("spam", "eggs", "yum")
args = dict([key,val for key,val in args.elements() if key in
allowedParms])

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top