From: fuglyducky on
On Aug 11, 9:31 am, Pinku Surana <sura...(a)gmail.com> wrote:
> On Aug 11, 12:07 pm, fuglyducky <fuglydu...(a)gmail.com> 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
>
> > def gen_nia(sample_string):
> >     NIA = """
> >     anothermultilinestringhere
> >     """
>
> >     sample_string += NIA
> >     return sample_string
>
> > gen_header(sample_string)
> > gen_nia(sample_string)
>
> > print(sample_string)
>
> There are 2 problems with your program.
>
> (1) If you want to use a global variable in a function, you have to
> add the line "global sample_string" to the beginning of that
> function.
>
> (2) Once you do (1), you will get an error because you've got
> sample_string as a global and a function parameter. Which one do you
> want to use in the function? You should change the name of the
> parameter to "sample" to solve that confusion.
>
> Here's the result, which works for me:
>
> sample_string = ""
> def gen_header(sample):
>     global sample_string
>     HEADER = """
>     mymultilinestringhere
>     """
>     sample_string = sample + HEADER
>     return sample_string
> def gen_nia(sample):
>     global sample_string
>     NIA = """
>     anothermultilinestringhere
>     """
>     sample_string = sample + NIA
>     return sample_string
> gen_header(sample_string)
> gen_nia(sample_string)
> print(sample_string)

Thanks! That did the trick.

I am a bit confused though. I tried to follow a sample in a book
(which works) where I didn't have to 1) pass the global variable as a
parameter into the function, 2) did not have to define the global
variable within the function. I apologize if this is a super stupid
question but if it is global, why do I have to pass it into the
function? Shouldn't the global variable be accessible from anywhere???
From: Wieland Hoffmann on
On 11.08.2010 18:07, fuglyducky wrote:
> Am I missing something??? Thanks in advance!!!!
Assign the returned value of your functions to something (like
sample_string) and it will work:
> ################################################
> # Global variable
> sample_string = ""
>
> def gen_header(sample_string):
> HEADER = """
> mymultilinestringhere
> """
>
> sample_string += HEADER
> return sample_string
>
> def gen_nia(sample_string):
> NIA = """
> anothermultilinestringhere
> """
>
> sample_string += NIA
> return sample_string
>
>
> sample_string = gen_header(sample_string)
> sample_string = gen_nia(sample_string)
>
> print(sample_string)

From: Jean-Michel Pichavant on
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
>
> def gen_nia(sample_string):
> NIA = """
> anothermultilinestringhere
> """
>
> sample_string += NIA
> return sample_string
>
>
> gen_header(sample_string)
> gen_nia(sample_string)
>
> print(sample_string)
>
sample_string = gen_header(sample_string)
sample_string = gen_nia(sample_string)

print sample_string

That should work.

I guess you made an error thinking that

sample_string += HEADER

would change the value of the given parameter. It does not. The value is
changed only for sample_string within the function This is the case for
any immutable object (strings are immutable) in Python.

Example:


class AMutable(object):
def __init__(self):
self.description = 'I am X'

myString = 'I am a immutable string' # an immutable object
myMutable = AMutable() # a mutable object

def bar(myMutable):
myMutable.description = 'I am Y' # change the attribute description
of the object

def foo(aString):
aString = 'fooooooooo' # will have no effect outside the function

print 'before calling bar: ', myMutable.description
bar(myMutable)
print 'after calling bar: ', myMutable.description
print 'before calling foo: ', myString
foo(myString)
print 'after calling foo: ', myString


>
before calling bar: I am X
after calling bar: I am Y
before calling foo: I am a immutable string
after calling foo: I am a immutable string

cheers,

JM
From: EW on
On Aug 11, 12:39 pm, fuglyducky <fuglydu...(a)gmail.com> wrote:
> On Aug 11, 9:31 am, Pinku Surana <sura...(a)gmail.com> wrote:
>
>
>
>
>
> > On Aug 11, 12:07 pm, fuglyducky <fuglydu...(a)gmail.com> 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
>
> > > def gen_nia(sample_string):
> > >     NIA = """
> > >     anothermultilinestringhere
> > >     """
>
> > >     sample_string += NIA
> > >     return sample_string
>
> > > gen_header(sample_string)
> > > gen_nia(sample_string)
>
> > > print(sample_string)
>
> > There are 2 problems with your program.
>
> > (1) If you want to use a global variable in a function, you have to
> > add the line "global sample_string" to the beginning of that
> > function.
>
> > (2) Once you do (1), you will get an error because you've got
> > sample_string as a global and a function parameter. Which one do you
> > want to use in the function? You should change the name of the
> > parameter to "sample" to solve that confusion.
>
> > Here's the result, which works for me:
>
> > sample_string = ""
> > def gen_header(sample):
> >     global sample_string
> >     HEADER = """
> >     mymultilinestringhere
> >     """
> >     sample_string = sample + HEADER
> >     return sample_string
> > def gen_nia(sample):
> >     global sample_string
> >     NIA = """
> >     anothermultilinestringhere
> >     """
> >     sample_string = sample + NIA
> >     return sample_string
> > gen_header(sample_string)
> > gen_nia(sample_string)
> > print(sample_string)
>
> Thanks! That did the trick.
>
> I am a bit confused though. I tried to follow a sample in a book
> (which works) where I didn't have to 1) pass the global variable as a
> parameter into the function, 2) did not have to define the global
> variable within the function. I apologize if this is a super stupid
> question but if it is global, why do I have to pass it into the
> function? Shouldn't the global variable be accessible from anywhere???

If it's a global then you don't have to pass it to the function but
you do have to have the line that says:
global sample_string


Now if you think the example in the book didn't do that and it still
worked then if you post that sample I'm sure somebody can tell you why
it worked. The book example might be doing something different.
From: Dave Angel on
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
>
> def gen_nia(sample_string):
> NIA = """
> anothermultilinestringhere
> """
>
> sample_string += NIA
> return sample_string
>
>
> gen_header(sample_string)
> gen_nia(sample_string)
>
> print(sample_string)
>
>
It'd be best if you used different names for global scope than you do
inside your functions. It won't change how this case works, but at
least it'd be clearer what's happening. And sometimes you can get an
unintended side effect when you use the same name for two different
variables.

In function gen_header(), you take an argument, and return a modified
version of it. But the call to it never uses the return value. If you
want to make any changes to the global value, you'd do something like this:

sample_string = gen_header(sample_string)
sample_string = gen_nia(sample_string)

print(sample_string)


HTH
DaveA