Python Forum
local namespace vs. global namespace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local namespace vs. global namespace
#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


Messages In This Thread
local namespace vs. global namespace - by Skaperen - Jul-01-2017, 02:59 AM
RE: local namespace vs. global namespace - by DeaD_EyE - Jul-02-2017, 11:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  which namespace? Skaperen 5 251 May-28-2024, 05:38 AM
Last Post: Gribouillis
  It's saying my global variable is a local variable Radical 5 1,327 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 2,011 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,125 Jan-27-2023, 05:32 PM
Last Post: caslor
  How to use global value or local value sabuzaki 4 1,240 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  'namespace' shorthand for function arguments? shadowphile 5 2,710 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Global vs. Local Variables Davy_Jones_XIV 4 2,735 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,412 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 2,409 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,547 Aug-28-2020, 09:24 AM
Last Post: Winfried

Forum Jump:

User Panel Messages

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