Python Forum
[SOLVED] Recommended way to declare global variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Recommended way to declare global variable?
#1
Question 
Hello,

To avoid the UnboundLocalError, out of curiosity, can't a global variable be declared somehow within an objet instead of at the top level and having to use "global"?

Thank you.

URL = "https://www.acme.com"

class MyFrame(wx.Frame):
	#No change
	#URL = "https://www.acme.com"
	
	def Dload(self):
		#UnboundLocalError: cannot access local variable 'URL' where it is not associated with a value
		#response = requests.get(URL)

		#No change
		#response = requests.get(self.URL)

		#fixed. Is there a better way?
		global URL
		response = requests.get(URL)
		...
--
Edit: Is there a way to avoid Cloudfare's annoying "Performing security verification - This website uses a security service to protect against malicious bots. This page is displayed while the website verifies you are not a bot." ?

--
Edit: Turns out this does work:

import wx

class ListBoxFrame(wx.Frame):
	URL = "https://www.acme.com"

	def Dload(self):
		print("URL/2:",self.URL)

	def __init__(self, *args, **kwargs):  
		super().__init__(None, wx.ID_ANY,size=(1000,400), title='blah')

		print("URL/1:",self.URL)
		self.Dload()
		
		panel = wx.Panel(self, -1)
		panel.Layout()

app = wx.App()
ListBoxFrame().Show()
app.MainLoop()
Reply
#2
There's no need for the "global" in this example.
URL = "https://www.acme.com"
 
class MyFrame(wx.Frame):
    def Dload(self):
        global URL
        response = requests.get(URL)
Python will first look for URL as a local variable (assigned in Dload), then it will look for URL in the global name space (defined in the module, outside a class definition). global would be needed if Dload assigned a value to URL.


In this example URL is not a global variable, it is a class variable.
class ListBoxFrame(wx.Frame):
    URL = "https://www.acme.com"
To access URL, you need to use a class reference
print(ListBoxFrame.URL)
You can also use an instance (like self) to reference URL. Python first looks for an instance variable named URL, then looks for a class variable.

As with the global variable example, a class variable acts differently when doing assignment. This code might produce unexpected results.
class Example:
    URL = "https://www.acme.com"
 
    def set_url(self, url):
        self.URL = url

a = Example()
b = Example()
b.set_url("not a url")
print(a.URL, Example.URL, b.URL)
Output:
https://www.acme.com https://www.acme.com not a url
The set_url method does not modify the class variable URL. It creates a new instance variable named URL. The class URL and instance URL are different objects and can have different values. The class URL is visible to all ListBoxFrame instances. The instance URL variable only visible to ListBoxFrame instances where set_url() was called. Such behavior is normally considered a bug.

You asked for recommendations on how to declare a global variable. My advice is to not use global variables. I would write you example like this.
import wx
 
class ListBoxFrame(wx.Frame):
    def Dload(self):
        print("URL/2:",self.url)
 
    def __init__(self, url, *args, **kwargs):  
        super().__init__(None, wx.ID_ANY,size=(1000,400), title='blah')
        self.url = url
        self.Dload()
        panel = wx.Panel(self, -1)
        panel.Layout()
 
app = wx.App()
ListBoxFrame("https://www.acme.com").Show()
app.MainLoop()
If ListBoxFrame can only work using www.acme.com I would consider making URL a class variable (and changing the class name to indicate its limited reusability).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Recommended way to upgrade module? Winfried 3 66 Apr-19-2026, 11:57 AM
Last Post: noisefloor
Question Recommended data structure for this? Winfried 6 190 Feb-16-2026, 11:05 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 18,941 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  It's saying my global variable is a local variable Radical 5 11,325 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  SOLVED variable as tuple name krayon70 7 3,995 Apr-09-2022, 03:30 PM
Last Post: krayon70
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 3,985 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE
  [solved] subdictionaries path as variable paul18fr 4 4,198 May-18-2021, 08:12 AM
Last Post: DeaD_EyE
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 9,992 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  Variable scope - "global x" didn't work... ptrivino 5 5,340 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 3,509 Sep-18-2020, 02:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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