Python Forum
changing variable outside of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing variable outside of a function
#7
Larz60+ Wrote:You should make a case and pep to suggest it be removed from the language.
It absolutely should not be removed from the language, but this isn't a good use case for it. This argument is just as valid as asking why I don't write a pep to remove eval/exec. They serve a purpose; this isn't it.

One of the main reasons to use it is to define constants within a function. And after definition to not change them. Say you are programatically building up a large dictionary which you want to be a global constant but don't want any other variables needed in the building process defined globally. Even then you could just return and assign. It is almost never the right thing to do, and in code of new programmers I have NEVER seen a single case where it was.

The fact that the OP had already committed towards doing the wrong thing is even more a reason to try to correct them.

I actually do use the global keyword as can be seen here:
https://github.com/Mekire/pygame-samples...im.py#L217
The use in this situation is some preprocessing on an object that both can't be created until another module is initialized and requires a conversion in order to use efficiently. Not using the global keyword would mean I needed to init the module in the global namespace which I stylistically don't like. It is also defined in caps to indicate it is indeed to be treated as a constant.
Reply


Messages In This Thread
changing variable outside of a function - by tkj80 - Jan-04-2017, 11:32 PM
RE: changing variable outside of a function - by Mekire - Jan-05-2017, 03:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 731 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 639 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,428 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,020 Aug-07-2023, 05:58 PM
Last Post: Karp
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,693 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,080 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 1,502 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  Cant transfer a variable onto another function KEIKAS 5 1,949 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,387 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,268 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