setattr question

bruno at modulix onurb at xiludom.gro
Fri Mar 3 03:51:28 EST 2006


Gerard Flanagan wrote:
> bruno at modulix wrote:
> 
(snip french)
> 
> I was trying to implement the factory pattern.
> The recipe above uses 'apply' which is deprecated according to the
> docs, and I suppose I was curious how to do the same sort of thing
> without 'apply'.

def fun(*args, **kwargs):
  pass

def otherfun(*args, **kwargs):
  fun(*args, **kwargs)

> 
>>Err... I don't see the point of this class. Why not just calling the
>>HtmlPage|Element|Literal classes directly ?
>>
>>
(snip)

>>'to_string' ?
>>Let's see... 'to_string', a class with only staticmethods in it...
>>You're coming from Java, aren't you ?-)
>>
> 
> Never been to Java in my life!

bad guess.

> I don't know if I understand you, HtmlElement has a 'to_string' method

I don't know this HtmlElement class, nor where it comes from.
'to_string()' (or is it toString() ?) is javaish. The pythonic idiom for
this is implementing the __str__() method and calling str(obj) on the
obj (which will call obj.__str__()). Hence my (bad) guess about you
being a Javaer.

> but no static methods:

Not the HtmlElement class, but the HtmlBuilder one. Here again, a class
defining only staticmethods is a pretty javaish idiom - in Python, we
just use good old functions !-) (in a separate module if you want a
separate namespace).

> class HtmlElement(list):
> 
>     def __init__(self, tag, text=None, **attrib):
>         self.tag = tag
>         self.text = text
>         self.attrib = attrib
> 
>     def write(self, writer):
>         writer.start(self.tag, self.attrib)
>         if self.text is not None:
>             writer.data(self.text)
>         for node in self:
>             node.write(writer)
>         writer.end()
> 
>     def to_string(self):
>         out = StringIO()
>         writer = HtmlWriter(out)
>         self.write(writer)
>         ret = out.getvalue()
>         out.close()
>         return ret
> 

If it's your own code, you may want to rename this last method __str__().

>>
>>>ie. to pass along *args and **kwargs to the HtmlElement constructor.
>>>Any suggestions?
>>
>>yes : pass along *args and **kwargs to the HtmlElement constructor !-)
>>
>>
>>>Or is there a better way to this kind of thing?
>>
>>yes again : kiss (Keep It Simple Stupid)
>>
>>There's not enough code to really grasp what you're trying to do, but
>>from what I see, I'd say you're having a bad case of arbitrary
>>overcomplexification.
>> 
> 
> My code itself is just a learning project ( etant sans emploi a ce
> moment, moi-meme...) and it is no doubt a bit 'over-egged' at the
> minute, but it is a first attempt and I can always refactor later.

My experience is that it's far easier to start simple and add
flexibility where needed than to simplify useless complexity. KISS,
yagni and all that kind of things...

> 
> Merci bien pour votre reponse.
> 
You're welcome.

BTW, there's also a french speaking python newsgroup at fr.comp.lang.py.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list