how to use setattr in __init__ method
Hi all, I want to clean up my code by setting my arguments dynamically at initialization. I use the manage_afterAdd method for this, but isn't there an other way to do this? This is my code now: def __init__(self, id, title=''): """Initialization Product, a form is added at init""" self.form = self.init_form() self.Add_Form() def manage_afterAdd(self, item, container): """magical method""" for (name, value) in self.REQUEST.form.items(): setattr(self, name, value) This is my add Form ( in short ): <dtml-var manage_page_header> <dtml-var manage_tabs> <form action="addMail" method="post" enctype="text/html"> <input type="hidden" name="intro" value=""> <input type="hidden" name="subscription" value=""> Id <input type="text" name="id" size="40" /> Title <input type="text" name="title" size="40" /> <input class="form-element" type="submit" name="submit" value=" Add "> </form> <dtml-var manage_page_footer> Can't this also be done with: def __init__( self, id='', **kw ): """initialization simple object""" self.id = id for (key,value) in kw.items(): setattr(self, key, value) Greetz Nico
Nico de Boer <nico@nfg.nl> wrote:
Hi all,
I want to clean up my code by setting my arguments dynamically at initialization. I use the manage_afterAdd method for this, but isn't there an other way to do this?
You should do your initialization in addMail by passing it the correct list of arguments to the constructor, and have __init__ do the initialization. manage_afterAdd will be called when pasting an object too, so you don't want to use that. Florent
This is my code now:
def __init__(self, id, title=''): """Initialization Product, a form is added at init""" self.form = self.init_form() self.Add_Form()
def manage_afterAdd(self, item, container): """magical method""" for (name, value) in self.REQUEST.form.items(): setattr(self, name, value)
This is my add Form ( in short ):
<dtml-var manage_page_header> <dtml-var manage_tabs>
<form action="addMail" method="post" enctype="text/html">
<input type="hidden" name="intro" value=""> <input type="hidden" name="subscription" value="">
Id <input type="text" name="id" size="40" />
Title <input type="text" name="title" size="40" />
<input class="form-element" type="submit" name="submit" value=" Add ">
</form>
<dtml-var manage_page_footer>
Can't this also be done with:
def __init__( self, id='', **kw ): """initialization simple object""" self.id = id for (key,value) in kw.items(): setattr(self, key, value)
Greetz Nico
-- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 79 87 http://nuxeo.com mailto:fg@nuxeo.com
participants (2)
-
Florent Guillaume -
Nico de Boer