Python: Is there a way to make a variable ALWAYS global?

Viewed 100

I'm writing a script that is starting to accumulate a significant number of functions that call other functions. There are variables that serve as global counters, or keep track of a frequently updated rotation angle, or to store assets that get referenced by the various functions. So a lot of my functions are ending up with up to 7 lines of code just to specify that these variables are global.

It's getting to be redundant. To use an analogy, when talking about The Statue of Liberty in a conversation you wouldn't begin with "The Statue of Liberty, on Liberty Island, within the borough of Manhattan, in the City of New York, within the state of New York... not the tiny one inside the snow globe on my mantle. Anyway, about that biggest and most famous Statue of Liberty, what I want to tell you is..." And imagine if every time you brought up the statue you had to be that specific because there was no way to simply say THE Statue of Liberty and be understood. That's where I feel like I am with Python right now, constantly having to tell my functions that many of my variables are global.

I'm a beginner with Python, so I'm probably missing something obvious. Is there a better way to approach this?

One thought I had is that maybe I should pass more variables through functions as arguments, but then I find it harder to keep track of the route that the variables travel, as opposed to just having to know their current state, like when they're global.

In my wishful thinking, there could be some syntax like: alwaysglobal my_var that I could use to tell all the functions that my_var will never be local. I suspect such a thing does not exist though.

0 Answers
Related