How to make variables global by default, Python 3.11

I have Python 3.11 on Windows 10 Pro. I’m still a bit new to Python but I’m learning.

In Python 3.11 the variables declared in the main program are not global by default and I cannot use them in functions. So if I want to use them in a function I have to use the global prefix/keyword.

# In main block.
myvar = ''
# Here's my function.
def myfunc():
    global myvar
    myvar = "hi"
    return myvar

in Python 3.11 is there a way to make all these variables global by default. I have about 5 variables used for command line options that I pass into some functions.


p.s. I can’t use Python 3.12 because Microsoft Azure does not support it yet. Azure will be my production environment.

EDIT: I currently have 8 global variables related to command-line options. It’s possible I will add a few more before the program is done.

No, and for good reason. Global variables should be avoided like the plague.

1 Like

I use an Option class to hold all the command options for my apps.
That way I can pass one object around.

They are global by default (if such a thing exists). The names just get rebound to local values when you reassign them… One way to avoid that is (sticking by your example):

def myfunc():
    newvar = myvar
    newvar = "hi"
    return newvar

or just pass them in to the function e.g.

    def myfunc(myvar=myvar):

(I’m assuming you want to use them under the same name)

I agree that you should try to keep the use of globals to a minimum as to avoid losing track of how they are used.
Personally I tend to use a top function in which the others are called but the issue remains the same: reusing a variable name and changing the value makes it a new variable

Me too. - Cameron Simpson

I was just thinking this. Right now I have 8 global variables in my program It’s possible I will add more.

Did you make your own class for command line options?

But what if I yearn for the good 'ole VIC-20 BASIC programming days of yore? haha.

Yes, I’m that old. I’m glad the days of limited memory are gone. I only have one case of a Perl program that actually runs out of memory on the server.

Either I create a class Options or add the options as variables to my class Application as self.opt_xxx. All depends on the complexity of the code.

Then maybe you’re that 10x BASIC developer that didn’t ever have to fix nightmare bugs in spaghetti code, or perhaps you’re looking back through rose tinted spectacles.

With good use of scopes to rule out bugs (making global variables read only if not in global scope, especially if known at compile time), I’d have thought modern compilers can reduce memory use for you.

I do this often enough that I have my own class.

But for trivial stuff you can just make a SimpleNamespace instance:

 from types import SimpleNamespace

 options = SimpleNamespace(dry_run=False, verbose=False)

and then use options.verbose etc as desired.

I write enough command line tools that I’ve got my own class, both for
the tool (BaseCommand) and the options (BaseCommandOptions) because
I amost always want usage messages, -h/-help/--help/help and
various common options like -v (verbose). So I subclass these for some
new CLI tool. Sketch of typical use:

 from dataclasses import dataclass
 import sys
 from cs.cmdutils import BaseCommand

 def main(argv=None):
     return MyCommand(argv).run()

 class MyCommand(BaseCommand):

     class Options(BaseCommand.Options):
         # some additional numeric setting
         extra_setting: int = 0
         # some additions mode
         extra_mode: str = 'simple'

 if __name__ == '__main__':

and some extra stuff, natably a main method for a basic command or a
suite of cmd_foo methods for subcommands foo etc.