Gerard Flanagan wrote: > Hello > > I have the following code: > > #### builder.py ######### > class HtmlBuilder(object): > > @staticmethod > def page(title=''): > return HtmlPage(title) > > @staticmethod > def element(tag, text=None, **attribs): > return HtmlElement(tag, text, **attribs) > > @staticmethod > def literal(text): > return HtmlLiteral(text)
Je ne vois pas très bien à quoi sert cette classe (à moins bien sûr qu'il y ait d'autre code). Pour ce que je vois là, pourquoi ne pas appeler directement les classes HtmlPage, HtmlElement et HtmlLiteral ? Err... I don't see the point of this class. Why not just calling the HtmlPage|Element|Literal classes directly ? > class HtmlElementFactory(object): > > def __init__(self): > for tag in ['li', 'ul']: > setattr( self, tag, HtmlBuilder.element(tag) ) > > ######################### > > and so I can do the following: > > html = HtmlElementFactory() > ul = html.ul > ul.attrib['class'] = 'default' > for i in range(3): > li = html.li > li.text = 'ghfhj' > ul.append(li) > print ul.to_string() > > but what I'd like to do is: > > html = HtmlElementFactory() > ul = html.ul( class='default' ) > for i in range(3): > ul.append( html.li( 'ghfhj' ) > print ul.to_string() 'to_string' ? Let's see... 'to_string', a class with only staticmethods in it... You're coming from Java, aren't you ?-) (if yes, google for "python is not java", it may be helpful) > 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. What's wrong with: # nb: class is a reserved word in Python ul = HtmlElement('ul', css_class='default') for i in range(3): ul.append(HtmlElement('li', 'baaz%d' % i) # nb2: use the __str__() method of HtmlElement print str(ul) Python's philosophy is to make simple things simple (don't worry, there's still space for complex things -> descriptors, metaclasses etc). HTH -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list