Updating a variable problem.

MRAB python at mrabarnett.plus.com
Tue Aug 4 13:46:16 EDT 2020


On 2020-08-04 18:25, Steve wrote:
> How is MSN a new variable? It is not intended to be.
> 
> If line 8 is commented out, it is printable in all three locations.
> 
> If line 8 is not commented out, then MSN in the previous line is determined to be undeclared.
> 
> It looks as if I am not allowed to change the contents of the variable MSN.
> 
>   
If there's an assignment to a variable anywhere in a function, then that 
variable is assumed to be local to that function unless it's explicitly 
declared global.

In 'EditTheEQlist', you're assigning to 'MSN' on the third line, and 
you're not declaring that it's global in that function, so it's assumed 
to be local. However, on the second line you're trying to use its value, 
but you haven't assigned to it yet.

You declared that 'MSN' was global in 'ReadTheEQfile', but that's a 
different function. 'global' applies only to the function it's used in.

> 
> From: Souvik Dutta <souvik.viksou at gmail.com>
> Sent: Tuesday, August 4, 2020 5:12 AM
> To: Steve <Gronicus at sga.ninja>
> Subject: Re: Updating a variable problem.
> 
>   
> 
> I don't know your exact meaning of fail. But as much as I can say there is a new variable MSN being declared in the function that is only seen locally that is inside the function. Now python sees this and probably says variable used before assigning. You might try declaring a global msn in the function again. And then changing msn after the print statement. Also this error occurred because python first searches the variable in the local scope which is absent earlier and so it searches for the variable in the global scope where it is present and so no errors are raised.
> 
> Souvik flutter dev
> 
>   
> 
> On Tue, Aug 4, 2020, 2:30 PM Steve <Gronicus at sga.ninja <mailto:Gronicus at sga.ninja> > wrote:
> 
> The print statement works in the EditTheEQlist() therefore the variable is seen as having been declared.  If I replace the value in the variable in the next line then the print statement fails. Is this still a matter of globality?
> 
> If it is, how do I fix it?
> 
> FootNote:
> If money does not grow on trees, then why do banks have branches?
> 
> From: Souvik Dutta <souvik.viksou at gmail.com <mailto:souvik.viksou at gmail.com> >
> Sent: Tuesday, August 4, 2020 4:50 AM
> To: Steve <Gronicus at sga.ninja <mailto:Gronicus at sga.ninja> >
> Cc: Python List <python-list at python.org <mailto:python-list at python.org> >
> Subject: Re: Updating a variable problem.
> 
>   Probably because the MSN variable in the second function is not global.
> 
>   On Tue, Aug 4, 2020, 2:08 PM Steve <Gronicus at sga.ninja <mailto:Gronicus at sga.ninja> > wrote:
> 
> Why should line 6 fail until line 7 is commented out?
> Python complains that MSN is "referenced before assignment".
> 
> def ReadTheEQfile():
>    global MSN
>    MSN = ("1 Monitor") #This line works every time.
> 
> def EditTheEQlist():
>    print("MSN2 = " + MSN) # Works if the next line is commented out.
>    MSN = ("3 Monitor")
> 
> # Main()
> ReadTheEQfile()
> print("MSN1 = " + MSN) # This line works every time
> EditTheEQlist()
> 


More information about the Python-list mailing list