Python Forum
local namespace vs. global namespace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local namespace vs. global namespace
#11
Additional: Don't use global. The lookup for globals take more time, as referencing to a local name (variable). Avoid this. Using global is a marker, that your program is broken by design.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
yes, a better way to have done it to avoid the error.

but all this was unknown to me at the time i encountered the UnboundLocalError problem. an what i had read about UnboundLocalError didn't lead me directly to any clues. but i was suspicious. as soon as i gathered the clue that def was the same as an assignment for this purpose (to decide what is local). then i removed the function nullification code and the problem went away. no one here suggested looking in that directio, so they either had not read this thread or didn't realize how wide the UnboundLocalError problem scope could be. i had done a lot of other things between adding that to my script and getting UnboundLocalError so it didn't come across as "recent changes broke it". oh well, live and learn.

(Jul-02-2017, 08:53 AM)DeaD_EyE Wrote: Additional: Don't use global. The lookup for globals take more time, as referencing to a local name (variable). Avoid this. Using global is a marker, that your program is broken by design.

where do you put your functions?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
The major danger, and difficult to find is when a global is changed in a thread,
while that same global is being used in another thread, and of course becomes more
complicated if used by more than two threads.

So much better to pass the value so the scope is protected.
Reply
#14
(Jul-02-2017, 08:56 AM)Skaperen Wrote: where do you put your functions?

def func1(foo, setting1, setting2):
   foo.do_something(setting1, setting2)

#then calling later
func(foo, setting1, setting2)

#instead of
def func1():
   foo.do_something(setting1, setting2)
   # calls the method do_something of global foo and uses the agruments setting1 and setting2, which are also looked up global
Your problem with UnboundLocal was a different.

n = 0

def bar():
   n = n + n
   return n
With the first assignment of a name, the Python interpreter looks, if the name exists locally (n + n). In this case, the answer is no.
It can't be assigned, because the expression looks up the name n and it doesn't exist as local variable.

Different example which works:

n = 10

def bar():
   global n
   n = n + n
   return n
But this function has a side effect. It turns the global variable into 20.
The returning value is the same like the global value. Avoid this.

You'll see this very often in tutorials for tkinter.

from tkinter import Tk, Button

root = Tk()

def callback():
   root.destroy()

Button(root, text='Quit', command=callback).pack()
root.mainloop()
better is:

from tkinter import Tk, Button

root = Tk()

def callback(root):
   root.destroy()

Button(root, text='Quit', command=lambda: callback(root)).pack()
root.mainloop()
lamda: callback(root) is an anonymous function. It's not called directly. It's called, when pressing the button.

A more elegant way without lamba:

from functools import partial
from tkinter import Tk, Button

root = Tk()

def callback(root):
  root.destroy()

Button(root, text='Quit', command=partial(callback, root)).pack()
root.mainloop()
https://docs.python.org/3.6/library/functools.html#functools.partial
https://docs.python.org/3/tutorial/contr...xpressions # in the example with lamda, it takes no arguments
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,187 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,808 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,041 Jan-27-2023, 05:32 PM
Last Post: caslor
  How to use global value or local value sabuzaki 4 1,168 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  'namespace' shorthand for function arguments? shadowphile 5 2,617 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Global vs. Local Variables Davy_Jones_XIV 4 2,675 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,282 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 2,338 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,453 Aug-28-2020, 09:24 AM
Last Post: Winfried
  local / global lists RedWuff 1 1,888 May-26-2020, 03:11 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020