Confused: Newbie Function Calls
Robert Kern
robert.kern at gmail.com
Wed Aug 11 12:33:56 EDT 2010
On 8/11/10 11:07 AM, fuglyducky wrote:
> I am a complete newbie to Python (and programming in general) and I
> have no idea what I'm missing. Below is a script that I am trying to
> work with and I cannot get it to work. When I call the final print
> function, nothing prints. However, if I print within the individual
> functions, I get the appropriate printout.
>
> Am I missing something??? Thanks in advance!!!!
>
> ################################################
> # Global variable
> sample_string = ""
>
> def gen_header(sample_string):
> HEADER = """
> mymultilinestringhere
> """
>
> sample_string += HEADER
> return sample_string
By default, all assignments inside of a function are local to the function. Even
augmented assignments like +=. Python strings are immutable, so
sample_string += HEADER
works exactly like
sample_string = sample_string + HEADER
The string referred to by the global name sample_string is never modified and
the global name is never reassigned.
http://docs.python.org/py3k/tutorial/classes.html#python-scopes-and-namespaces
> gen_header(sample_string)
> gen_nia(sample_string)
>
> print(sample_string)
You probably want something like the following:
sample_string = gen_header(sample_string)
sample_string = gen_nia(sample_string)
print(sample_string)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list