By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 10 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jean-Paul Calderone wrote:
On 10 Jan 2007 14:46:54 -0800, Emin <em**********@gmail.comwrote:
>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...uctlike.py#L35
If you don't want to go that far then this might give you an idea or two:
>>class example:
... 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
...
>>x = example(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
self
a
b
c
d
e
f
g
h
i
j
k
l
m
n
>>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com

Jan 11 '07 #2

P: n/a
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

Jan 11 '07 #3

P: n/a
Emin wrote:
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] = i

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

Luis

Jan 11 '07 #4

P: n/a
def __init__(self, **kw):

self.argdict = kw
Emin wrote:
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
Jan 11 '07 #5

P: n/a
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

On Jan 10, 9:05 pm, "Luis M. González" <luis...@gmail.comwrote:
Emin wrote:
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] = i

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

Luis
Jan 11 '07 #6

P: n/a
"Emin" <em**********@gmail.comwrote:
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.
http://effbot.org/pyref/setattr

</F>

Jan 11 '07 #7

P: n/a
Emin wrote:
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

--
BOFH excuse #64:

CPU needs recalibration

Jan 11 '07 #8

P: n/a
Thanks, that looks like what I wanted.

On Jan 11, 8:36 am, "Fredrik Lundh" <fred...@pythonware.comwrote:
"Emin" <emin.shop...@gmail.comwrote:
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. http://effbot.org/pyref/setattr

</F>
Jan 11 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.