setattr question
Gerard Flanagan
grflanagan at yahoo.co.uk
Fri Mar 3 10:42:30 EST 2006
bruno at modulix wrote:
> Gerard Flanagan wrote:
> > The functions were kind of related
> > and meaningless outside the module they were declared -
>
> FWIW (and from the snippet I saw), these functions are useless even in
> the module !-)
>
ok, ok... :-)
> Unless you want to dynamically choose the concrete class at runtime
> based on platform/settings/phase of the moon/whatnot (which seems not to
> be te case in the snippet you posted), you don't need these functions,
> just instanciating the concrete class is enough. Remember that Python
> classes *are* factory already - and that you can freely replace a class
> by any callable returning an instance, ie:
>
> == before refactoring, directly instanciating concrete class ==
> # myhtmlmodule.py
> class HtmlElement(tag, *args, **kw):
> # code here
>
> # myclientmodule.py
> from myhtmlmodule import HtmlElement
> ul = HtmlElement('ul')
>
>
> == after refactoring, using a factory function ==
> # myhtmlmodule.py
> class _HtmlElement1(tag, *args, **kw):
> # code here
>
> class _HtmlElement2(tag, *args, **kw):
> # other code here
>
> # yes, it's now a function...
> def HtmlElement(tag, *args, **kw):
> if phase_of_the_moon():
> klass = _HtmlElement1
> else:
> klass = _HtmlElement2
> return klass(tag, *args, **kw)
>
> # myclientmodule.py
> # well... nothing changed here !-)
> from myhtmlmodule import HtmlElement
> ul = HtmlElement('ul')
>
ah, I'm getting it.
>
> > I've ditched the factory class in any case:
> >
> > http://gflanagan.net/site/python/htmlbuilder/HtmlBuilder.py
> > (FWIW)
>
> Seems mostly clean. May I suggest a couple small corrections/improvements ?
>
> 1/ potential bugfix:
> try:
> from tidy import parseString
> except ImportError:
> def parseString(text):
> # woops, this function is supposed to return something
> #pass
> return text
>
> 2/ safer and cleaner
> class HtmlPage(HtmlElement):
> # removed class vars,
> # replaced with default in __init__
> def __init__(self, title, **kw):
> self.title = title
> self.stylesheets = kw.get('stylesheets', [])
> self.doctype = kw.get('doctype', HTML4_STRICT)
>
That's much better - thanks very much for taking the time, I'm a little
bit wiser!
regards
Gerard
> --
> 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