Python Forum
How to create a menu button?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a menu button?
#31
Quote:I just read before typing these words how to use Global to access a variable from a function to another. Its not even explain in the book.

global is bad. Glad it is not in the book. I never use global.

Your statement is not quite correct. "global x" tells Python "x is a global variable, not a local variable. If I assign a value to x, do the assignment in the global scope, not the local scope". Functions cannot access each other's variables, but if your functions use global variables they can modify the value of those variables and other functions can see the modified value. And this characteristic of global variables is why they are bad. Global variables work fine when you have tiny little programs, but for software development on a larger scale it becomes difficult to keep track of all the places where a variable is used. When everything can see the same variables it is difficult to predict how changing code that assigns the variable value will affect other code that uses the variable. Fixing a bug can have unintended side affects, creating other bugs. Fixing those bugs create even more bugs.

For functions, local variables are the way to go. Assigning a local variable cannot affect any code outside the function. If other functions need to see the variable value, have the function return the value, or write a class and have the variable be an attribute of the class, or an instance of the class.
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I create menu in Python? khagan07 1 517 Oct-07-2023, 12:44 PM
Last Post: deanhystad
  Create a dynamic Menu from a editable Dictionary. KiNeMs 1 2,285 Jan-28-2020, 04:27 AM
Last Post: Larz60+
  Create menu with selectable items SaladFingers 4 2,897 Sep-28-2019, 11:23 AM
Last Post: SaladFingers

Forum Jump:

User Panel Messages

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