Re: Updating a variable problem.

2020-08-04 Thread dn via Python-list

On 04/08/2020 20:38, Steve 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()


NB there are no lineNRs above!
(added comment/guide == good job!)


Others have answered the question. Here is some reading to consolidate 
your understanding:-


What are the rules for local and global variables in Python?
https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

Assignment is defined recursively depending on the form of the target 
(list). When a target is part of a mutable object (an attribute 
reference, subscription or slicing), the mutable object must ultimately 
perform the assignment and decide about its validity, and may raise an 
exception if the assignment is unacceptable. The rules observed by 
various types and the exceptions raised are given with the definition of 
the object types (see section The standard type hierarchy).

...
If the target is an identifier (name):

If the name does not occur in a global or nonlocal statement in the 
current code block: the name is bound to the object in the current local 
namespace.


Otherwise: the name is bound to the object in the global namespace 
or the outer namespace determined by nonlocal, respectively.

https://docs.python.org/3/reference/simple_stmts.html#assignment-statements

...we have the function modifying x. It may appear somewhat confusing 
since x is being used in multiple locations...

https://pythonprogramming.net/global-local-variables/

"Scope", particularly in its applications to Classes and Namespaces:
https://docs.python.org/3/tutorial/classes.html

The "LEGB" 'rule' (NB not a term you'll find in the Python docs) ie 
Local, Enclosing, Global, and Built-in scopes:

https://realpython.com/python-scope-legb-rule/
and more in:
https://realpython.com/python-namespaces-scope/
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Updating a variable problem.

2020-08-04 Thread MRAB

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 
Sent: Tuesday, August 4, 2020 5:12 AM
To: Steve 
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 mailto:Gronicus@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 mailto:souvik.vik...@gmail.com> >
Sent: Tuesday, August 4, 2020 4:50 AM
To: Steve mailto:Gronicus@sga.ninja> >
Cc: Python List mailto:python-list@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 mailto:Gronicus@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()


--
https://mail.python.org/mailman/listinfo/python-list


RE: Updating a variable problem.

2020-08-04 Thread Steve
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.

 

FootNote:
If money does not grow on trees, then why do banks have branches?

 

From: Souvik Dutta  
Sent: Tuesday, August 4, 2020 5:12 AM
To: Steve 
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 mailto:Gronicus@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 mailto:souvik.vik...@gmail.com> > 
Sent: Tuesday, August 4, 2020 4:50 AM
To: Steve mailto:Gronicus@sga.ninja> >
Cc: Python List mailto:python-list@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 mailto:Gronicus@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()

=
Footnote:
Genie: You have three wishes.
Me: I wish I had more wishes.
Genie: You cannot wish for more wishes.
Me: I wish I could.


-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Updating a variable problem.

2020-08-04 Thread Souvik Dutta
Probably because the MSN variable in the second function is not global.

On Tue, Aug 4, 2020, 2:08 PM Steve  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()
>
>
>
> =
> Footnote:
> Genie: You have three wishes.
> Me: I wish I had more wishes.
> Genie: You cannot wish for more wishes.
> Me: I wish I could.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Updating a variable problem.

2020-08-04 Thread Steve
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()



=
Footnote:
Genie: You have three wishes.
Me: I wish I had more wishes.
Genie: You cannot wish for more wishes.
Me: I wish I could.


-- 
https://mail.python.org/mailman/listinfo/python-list