enhancing/wrapping an existing instance of a duck
Neville Dempsey
nevillednz at gmail.com
Mon Sep 1 01:06:45 EDT 2008
What do I need to add to HTMLDecorator?
A simpler example:
import cgi
class ClassX(object):
pass # ... with own __repr__
class ClassY(object):
pass # ... with own __repr__
inst_x=ClassX()
inst_y=ClassY()
inst_z=[ i*i for i in range(25) ]
inst_b=True
class HTMLDecorator(object):
def html(self): # an "enhanced" version of __repr__
return cgi.escape(self.__repr__()).join(("<H1>","</H1>"))
print HTMLDecorator(inst_x).html()
print HTMLDecorator(inst_y).html()
wrapped_z = HTMLDecorator(inst_z)
inst_z[0] += 70
wrapped_z[0] += 71
print wrapped_z.html()
print HTMLDecorator(inst_b).html()
Output:
Traceback (most recent call last):
File "html.py", line 21, in <module>
print HTMLDecorator(inst_x).html()
TypeError: default __new__ takes no parameters
Can I simply decorate an existing instance?
Cheers
N
More information about the Python-list
mailing list