Passing arguments by value...

Gordon McMillan gmcm at hypernet.com
Thu Feb 24 17:58:18 EST 2000


John F. Brainard wrote:

> I'm working on a set of HTML Layout classes for CGI programming
> in Python and have stumbled upon a small problem...
> 
> The sample code below should be self explanitory...
> 
> #begin python code
> html = body.Body()
> 
> font1 = tags.Font(4, "Arial", "#FF0000")
> font1.addText(string.strip("""
> 	Some text would go in here.
> """))
> 
> html.addElement(font)
> 
> font1.clearText()
> font1.addText(string.strip("""
> 	Some other text here.
> """))
> 
> print "<HTML>" + str(html) + "</HTML>"
> #end python code
> 
> After I create the font1 object and set it's properties, I add it
> to the html object. Then, I change the text and add it again to
> the html object. What I get as the output is...
> 
> #Begin HTML here
> <HTML>
> <body>
> <font size=1 face="Arial" color="#FF0000">
> Some other text here.
> </font>
> 
> <font size=1 face="Arial" color="#FF0000">
> Some other text here.
> </font>
> </HTML>
> #End HTML here
> 
> Is there a way to copy the font object or pass it by value rather
> than reference to the addElement() function?

You have a couple alternatives.

Make font.__repr__ return "<FONT>....</FONT>" and do
html.addElement(repr(font1)). Or just use a new instance of 
font for each separate element, (and drop the clearText 
method).

- Gordon




More information about the Python-list mailing list