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 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
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
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
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
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
"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>
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
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>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |