Python Forum
Trouble with global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with global variables
#8
Python does indeed have globals, but ...

Global variables are considered bad programming in all languages by seasoned programmers.

Here's one reason that kills their use (In my book):
Coupling between various methods (or functions). Assume the following:
  • Method 1 sets global variable snoot to 4
  • Method 2, which uses the contents of snoot, executes method 3 as a thread if snoot equals 4
        Upon return from the thread, it checks the content of  snoot,
        if snoot equals anything other than 4, it prints message: 'method 3 was not executed'
        otherwise it prints message: ''method 3 was successfully executed'.
  • While the thread in method 2 was running, method 5 changed the global variable snoot to 2.
        Method2 will see the '2' when thread completes, thus prints the wrong message.
There are many other reasons you should avoid using globals , see: http://wiki.c2.com/?GlobalVariablesAreBad
Reply


Messages In This Thread
Trouble with global variables - by PickyBiker - May-17-2017, 02:01 PM
RE: Trouble with global variables - by sparkz_alot - May-17-2017, 02:27 PM
RE: Trouble with global variables - by PickyBiker - May-17-2017, 02:36 PM
RE: Trouble with global variables - by snippsat - May-17-2017, 03:48 PM
RE: Trouble with global variables - by PickyBiker - May-17-2017, 04:15 PM
RE: Trouble with global variables - by buran - May-17-2017, 04:27 PM
RE: Trouble with global variables - by PickyBiker - May-17-2017, 04:36 PM
RE: Trouble with global variables - by Larz60+ - May-17-2017, 04:43 PM
RE: Trouble with global variables - by buran - May-17-2017, 04:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,302 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,193 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,293 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 1,056 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,996 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,807 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,532 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,482 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,641 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 3,257 Feb-09-2020, 02:05 PM
Last Post: new_to_python

Forum Jump:

User Panel Messages

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