473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
8 1095
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
14
by: Mac | last post by:
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 self.b = b self.c = c self.d = d ...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
7
by: p.lavarre | last post by:
Subject: announce: FAQs suggested ... That suggested FAQ is misleadingly incorrect as stated - we need help rewording it. /F correctly commented: "eval" is never a good choice if you cannot...
0
by: Sells, Fred | last post by:
I had some code originally that printed the sql and params when I called the .execute method. I removed it but it still prints. I rebooted and renamed files and still it prints. I am totally...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.