Here is what is going on.

First, the only global variables Python really has are module-scoped variables. You cannot make a variable that is truly global; all you can do is make a variable in a particular scope. (If you make a variable inside the Python interpreter, and then import other modules, your variable is in the outermost scope and thus global within your Python session.)

All you have to do to make a module-global variable is just assign to a name.

Imagine a file called foo.py, containing this single line:

X = 1

Now imagine you import it.

import foo
print(foo.X)  # prints 1

However, let's suppose you want to use one of your module-scope variables as a global inside a function, as in your example. Python's default is to assume that function variables are local. You simply add a global declaration in your function, before you try to use the global.

def initDB(name):
    global __DBNAME__  # add this line!
    if __DBNAME__ is None: # see notes below; explicit test for None
        __DBNAME__ = name
    else:
        raise RuntimeError("Database name has already been set.")

By the way, for this example, the simple if not __DBNAME__ test is adequate, because any string value other than an empty string will evaluate true, so any actual database name will evaluate true. But for variables that might contain a number value that might be 0, you can't just say if not variablename; in that case, you should explicitly test for None using the is operator. I modified the example to add an explicit None test. The explicit test for None is never wrong, so I default to using it.

Finally, as others have noted on this page, two leading underscores signals to Python that you want the variable to be "private" to the module. If you ever do an from mymodule import *, Python will not import names with two leading underscores into your name space. But if you just do a simple import mymodule and then say dir(mymodule) you will see the "private" variables in the list, and if you explicitly refer to mymodule.__DBNAME__ Python won't care, it will just let you refer to it. The double leading underscores are a major clue to users of your module that you don't want them rebinding that name to some value of their own.

It is considered best practice in Python not to do import *, but to minimize the coupling and maximize explicitness by either using mymodule.something or by explicitly doing an import like from mymodule import something.

EDIT: If, for some reason, you need to do something like this in a very old version of Python that doesn't have the global keyword, there is an easy workaround. Instead of setting a module global variable directly, use a mutable type at the module global level, and store your values inside it.

In your functions, the global variable name will be read-only; you won't be able to rebind the actual global variable name. (If you assign to that variable name inside your function it will only affect the local variable name inside the function.) But you can use that local variable name to access the actual global object, and store data inside it.

You can use a list but your code will be ugly:

__DBNAME__ = [None] # use length-1 list as a mutable

# later, in code:  
if __DBNAME__[0] is None:
    __DBNAME__[0] = name

A dict is better. But the most convenient is a class instance, and you can just use a trivial class:

class Box:
    pass

__m = Box()  # m will contain all module-level values
__m.dbname = None  # database name global in module

# later, in code:
if __m.dbname is None:
    __m.dbname = name

(You don't really need to capitalize the database name variable.)

I like the syntactic sugar of just using __m.dbname rather than __m["DBNAME"]; it seems the most convenient solution in my opinion. But the dict solution works fine also.

With a dict you can use any hashable value as a key, but when you are happy with names that are valid identifiers, you can use a trivial class like Box in the above.

Answer from steveha on Stack Overflow
Top answer
1 of 5
334

Here is what is going on.

First, the only global variables Python really has are module-scoped variables. You cannot make a variable that is truly global; all you can do is make a variable in a particular scope. (If you make a variable inside the Python interpreter, and then import other modules, your variable is in the outermost scope and thus global within your Python session.)

All you have to do to make a module-global variable is just assign to a name.

Imagine a file called foo.py, containing this single line:

X = 1

Now imagine you import it.

import foo
print(foo.X)  # prints 1

However, let's suppose you want to use one of your module-scope variables as a global inside a function, as in your example. Python's default is to assume that function variables are local. You simply add a global declaration in your function, before you try to use the global.

def initDB(name):
    global __DBNAME__  # add this line!
    if __DBNAME__ is None: # see notes below; explicit test for None
        __DBNAME__ = name
    else:
        raise RuntimeError("Database name has already been set.")

By the way, for this example, the simple if not __DBNAME__ test is adequate, because any string value other than an empty string will evaluate true, so any actual database name will evaluate true. But for variables that might contain a number value that might be 0, you can't just say if not variablename; in that case, you should explicitly test for None using the is operator. I modified the example to add an explicit None test. The explicit test for None is never wrong, so I default to using it.

Finally, as others have noted on this page, two leading underscores signals to Python that you want the variable to be "private" to the module. If you ever do an from mymodule import *, Python will not import names with two leading underscores into your name space. But if you just do a simple import mymodule and then say dir(mymodule) you will see the "private" variables in the list, and if you explicitly refer to mymodule.__DBNAME__ Python won't care, it will just let you refer to it. The double leading underscores are a major clue to users of your module that you don't want them rebinding that name to some value of their own.

It is considered best practice in Python not to do import *, but to minimize the coupling and maximize explicitness by either using mymodule.something or by explicitly doing an import like from mymodule import something.

EDIT: If, for some reason, you need to do something like this in a very old version of Python that doesn't have the global keyword, there is an easy workaround. Instead of setting a module global variable directly, use a mutable type at the module global level, and store your values inside it.

In your functions, the global variable name will be read-only; you won't be able to rebind the actual global variable name. (If you assign to that variable name inside your function it will only affect the local variable name inside the function.) But you can use that local variable name to access the actual global object, and store data inside it.

You can use a list but your code will be ugly:

__DBNAME__ = [None] # use length-1 list as a mutable

# later, in code:  
if __DBNAME__[0] is None:
    __DBNAME__[0] = name

A dict is better. But the most convenient is a class instance, and you can just use a trivial class:

class Box:
    pass

__m = Box()  # m will contain all module-level values
__m.dbname = None  # database name global in module

# later, in code:
if __m.dbname is None:
    __m.dbname = name

(You don't really need to capitalize the database name variable.)

I like the syntactic sugar of just using __m.dbname rather than __m["DBNAME"]; it seems the most convenient solution in my opinion. But the dict solution works fine also.

With a dict you can use any hashable value as a key, but when you are happy with names that are valid identifiers, you can use a trivial class like Box in the above.

2 of 5
136

Explicit access to module level variables by accessing them explicity on the module


In short: The technique described here is the same as in steveha's answer, except, that no artificial helper object is created to explicitly scope variables. Instead the module object itself is given a variable pointer, and therefore provides explicit scoping upon access from everywhere. (like assignments in local function scope).

Think of it like self for the current module instead of the current instance !

# db.py
import sys

# this is a pointer to the module object instance itself.
this = sys.modules[__name__]

# we can explicitly make assignments on it 
this.db_name = None

def initialize_db(name):
    if (this.db_name is None):
        # also in local function scope. no scope specifier like global is needed
        this.db_name = name
        # also the name remains free for local use
        db_name = "Locally scoped db_name variable. Doesn't do anything here."
    else:
        msg = "Database is already initialized to {0}."
        raise RuntimeError(msg.format(this.db_name))

As modules are cached and therefore import only once, you can import db.py as often on as many clients as you want, manipulating the same, universal state:

# client_a.py
import db

db.initialize_db('mongo')
# client_b.py
import db

if (db.db_name == 'mongo'):
    db.db_name = None  # this is the preferred way of usage, as it updates the value for all clients, because they access the same reference from the same module object
# client_c.py
from db import db_name
# be careful when importing like this, as a new reference "db_name" will
# be created in the module namespace of client_c, which points to the value 
# that "db.db_name" has at import time of "client_c".

if (db_name == 'mongo'):  # checking is fine if "db.db_name" doesn't change
    db_name = None  # be careful, because this only assigns the reference client_c.db_name to a new value, but leaves db.db_name pointing to its current value.

As an additional bonus I find it quite pythonic overall as it nicely fits Pythons policy of Explicit is better than implicit.

🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL. Always decide whether a class’s methods and instance variables (collectively: “attributes”) should be public or non-public.
Discussions

When should I use a global variable module in Python? - Software Engineering Stack Exchange
In Python projects, if your project spans multiple Python modules and you need to pass some data between the modules, as far as I know, there's 2 main ways: Pass the data as function arguments Cre... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
October 31, 2021
design - Python Class vs File level variables - Software Engineering Stack Exchange
If the constants are used outside of this module, then module-level variables have the advantage that you can import them. Class variables have the advantage that they are bundled in that class. This may provide a more convenient interface because you don't have to pass them around individually. For example, the Enum class does this nicely. But in reverse: why would you be bundling them unless they are used together? Class variables in Python ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 14, 2018
Are global (module level) variables bad?
Module ≠ global. Your are correct that Module (and everything declared at that level) is singleton by definition and this is one way of implementing singletons in Python. We can debate whether the singleton pattern itself is an anti-pattern until the cows come home, but regardless of our conclusion we all recognize that they exist. (There are other way to implement singletons, BTW. Using metaclasses, for example.) More to your question, we must declare things at Module level and its not inherently bad. Recall that class definitions are themselves instances of (usually) type objects in the python object model. If we blanket say that Module level declarations are bad, then we can, effectively, never use modules at all (which is absurd). Module level declarations are also frequently used to provide aliases to things that would otherwise not be in the modules names pace, which is one way we can easily manage how users will interact with our Module (IE: define the set of vars/functions/classes that are prominent in the API for the module). What is important to note is that anything you declare at Module level is instantiated when the module IS IMPORTED not when the declared item is accessed. So, you probably don't want to put the contents of a massive database into a variable here (not that you ever really should). You also probably dont want to do any seriously intensive computation here (IE: You dont want it to take 5min just to import the module to compute something that the user might not need). But defining some lightweight things that should be exposed to all module users is fine. Constants, functions, classes and so on. This is just a matter of design: would it be better to enclose a variable as a member of a class or as a member of the module? In most cases, if it will change over time, a smaller scope than the Module is preferred even if the variable is a singleton, but exceptions can and do exist. --- Unfortunately, the above is a bit of a long winded nonanswer. To summarize, its usually preferred to put things into a smaller scope than the module, but its still acceptable to put anything at module level if that is what your design calls for. If I just have to give a rule of thumb for beginners: you can put it at module level if it is constant, a function, a class, an alias or a simple singleton. If it doesn't fit one of those, its probably worth reconsidering your design. Ofc, as you advance, you absolutely can break this so long as you can explain why you are doing it; its guidance, not a rule. More on reddit.com
🌐 r/learnpython
25
17
August 22, 2025
Why not use global variables?
They add unneeded complexity and doubt into your code. Imagine you have a variable at the start of your program called "really_important" now imagine your code is 50,000 lines long and somewhere in there you import another module that also has a "really_important" global. Imagine trying to figure out which one is which, when they're used, when they're being modified, by whom, etc. Scope is a very powerful organizational tool. It helps you (and your IDE) remember what is important for any piece of code. For example: x = 0 y = 0 def add_x_y(): return x + y In the above you need to remember that this adds x and y together and inside the function you have zero assurance that x and y are even set. Contrasted with: def add_x_y(x, y): return x + y Not only is this shorter, the function prototype tells you exactly what you need to give it (two things named x and y), your IDE will helpfully provide you with insight about it, and the error you receive if you failed to define x or y properly will make a lot more sense. More on reddit.com
🌐 r/learnpython
31
21
July 27, 2021
🌐
LabEx
labex.io › tutorials › python-how-to-manage-module-global-variables-435508
How to manage module global variables | LabEx
In LabEx's recommended practices, it's often better to use alternative design patterns like dependency injection or class-based approaches. ## Global configuration example DEBUG_MODE = False MAX_CONNECTIONS = 100 def configure_debug(status): global DEBUG_MODE DEBUG_MODE = status def check_debug_status(): print(f"Debug mode is: {DEBUG_MODE}") configure_debug(True) check_debug_status() ## Output: Debug mode is: True · Variable scoping in Python defines the visibility and accessibility of variables within different contexts.
🌐
LabEx
labex.io › tutorials › python-how-to-define-global-variables-in-python-modules-398175
How to define global variables in Python modules | LabEx
Constants are variables that are defined at the module level and are intended to be immutable. This can help make your code more predictable and easier to reason about. By following these best practices, you can help ensure that your use of global variables in Python remains limited and ...
Top answer
1 of 1
8

Once upon a time in the early days of computers, there were no function parameters or return values. There were only global variables. This was an utter mess and completely unmaintainable. It is really difficult to track how data flows through such a “spaghetti code” program that uses global variables to communicate, because every part of the code could change everything.

Python is a very flexible languages, so it is possible to reach into another module and change its global variables (especially since there are no true constants in Python). But with great power comes great responsibility. It is rarely appropriate to do this. Legitimate examples I have seen include:

  • monkey-patching for tests
  • initializing and configuring singletons
  • manipulating process-global data such as sys.env

In the comments, you mention django.conf.settings. Since these settings are by design process-global (you can only have one Django app running per process) it's OK to access and modify those settings. However, if I had designed Django, I might have chosen another approach for configuring settings that does not rely on global variables.

For software that you are designing, it is probably better to make your data flows explicit and to pass data via function parameters and return values. If you have a lot of related data, you can create a class to group these variables. With Python 3.7 dataclasses, it is now very easy to create such classes. I find that this helps to write well-structured and easy to test code: all the data dependencies are explicit, which helps to figure out what has to be provided in a test case. If a function depends on a lot of unrelated stuff, this could indicate that the code has been structured in an awkward way.

For example, consider this code where we have some business logic to transform data and save it in a database:

DB_CONNECTION = ...
DATA = ...

def my_function() -> None:
  # some tricky business logic
  DATA['y'] += DATA['x']**2
  DB_CONNECTION.execute('INSERT INTO table VALUES(?, ?)', (DATA['x'], DATA['y']))

# caller:
DB_CONNECTION = ...
DATA = ...
my_function()

A caller of this function must make sure that the DB_CONNECTION and DATA variables are initialized before calling the function. Such “temporal dependencies” are bad API design in most cases. Here, we have also introduced an artificial constraint that there can only be one connection and one data in the program.

We could refactor this by turning those variables into function arguments. This makes it clear to the caller what has to be provided for the function to work. It also makes it much more straightforward to test the function:

def my_function(data: Data, *, conn: Connection) -> None:
  # some tricky business logic
  data['y'] += data['x']**2
  conn.execute('INSERT INTO table VALUES(?, ?)', (data['x'], data['y']))

# caller:
my_function(..., conn=...)

But why is this function taking both data and a database connection? For testing the tricky business logic, we would still have to provide a database connection and then read the data back from the database. In some cases, this can be simplified by splitting this code into a “pure” part that just manipulates our data, and a part that performs external interaction.

def my_inplace_transformation(data: Data) -> None:
  # some tricky business logic
  data['y'] += data['x']**2

# pure alternative that does not modify data:
def my_transformation(data: Data) -> Data:
  return { 'y': data['x']**2, **data }

def save_data(data: Data, *, conn: Connection) -> None:
  conn.execute('INSERT INTO table VALUES(?, ?)', (data['x'], data['y']))

# caller:
data = ...
my_inplace_transformation(data)
# alternative: data = my_transformation(data)
save_data(data, conn=...)

Here, the caller has regained a bit of responsibility: it's now the caller's job to pass the data between the functions, in particular to call the save_data() function if the data shall be saved. But now the main business logic is nicely isolated and very easy to test.

🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.14.3 documentation
This variable can be modified; doing so affects future searches for modules and subpackages contained in the package. While this feature is not often needed, it can be used to extend the set of modules found in a package. ... In fact function definitions are also ‘statements’ that are ‘executed’; the execution of a module-level function definition adds the function name to the module’s global namespace.
🌐
Real Python
realpython.com › python-use-global-variable-in-function
Using and Creating Global Variables in Your Python Functions – Real Python
December 8, 2024 - Note: While you can create global ... your code harder to understand and maintain. It’ll be better to define global variables at the module level outside ......
Find elsewhere
🌐
Python
docs.python.org › 3 › faq › programming.html
Programming FAQ — Python 3.14.3 documentation
If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation. This answer actually applies to all methods, but the question usually comes up first in the context of constructors. ... In Python you have to write a single constructor that catches all cases using default arguments. For example: class C: def __init__(self, i=None): if i is None: print("No arguments") else: print("Argument is", i) This is not entirely equivalent, but close enough in practice.
🌐
W3Schools
w3schools.com › python › gloss_python_module_variables.asp
Python Module Variables
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
🌐
Index.dev
index.dev › blog › how-to-set-global-variables-python
How to Set Global Variables Across Modules in Python
February 27, 2025 - Setting global variables across modules in Python can be done in multiple ways, from simple imports to using the singleton pattern. The best approach depends on your use case. If you need a quick solution, using a separate config module works well. If you need better control, using classes or singletons is a cleaner approach. By following best practices, you can manage global variables effectively without introducing unexpected bugs or performance issues. For Developers: Level up your career with remote opportunities!
🌐
Astrofrog
astrofrog.github.io › py4sci › _static › 09. Modules and Variable Scope.html
09. Modules and Variable Scope
This is very useful because it means that modules don't have to be imported inside functions, you can import them at the top level: ... This works because modules are objects in the same sense as any other variable. In practice, this does not mean that you should ever use:
Top answer
1 of 1
3

This is a mostly stylistic choice. In the end, it doesn't matter. There are some finer differences depending on how they are used.

  • If the constants are only used within the module, using module-level variables is more convenient. (Compare class variables below).

  • If the constants are used outside of this module, then module-level variables have the advantage that you can import them.

  • Class variables have the advantage that they are bundled in that class. This may provide a more convenient interface because you don't have to pass them around individually. For example, the Enum class does this nicely. But in reverse: why would you be bundling them unless they are used together?

Class variables in Python do have some gotchas.

  • Python doesn't have true constants, so anyone could reassign them. (To create read-only fields in Python, you must use a property, or more generally: the descriptor protocol).
  • These variables can be shadowed by subclasses, or by instance variables.
  • The variables can only be accessed through the class or its instances. E.g. to access the URL variable in a method, you have to say Domain.URL or self.URL. Module-level variables can be accessed directly.

In general, Python doesn't force you to stuff everything into a class. You should use this freedom because most variables or functions don't belong into a class. If in doubt, use free variables/functions instead of fields/methods within a class. You can always change your mind later and refactor.

🌐
Reddit
reddit.com › r/learnpython › are global (module level) variables bad?
r/learnpython on Reddit: Are global (module level) variables bad?
August 22, 2025 -

In other languages it is generally considered a very bad practice to define global variables and generally everyone avoids it. But when I read Python libraries/programs, I see it is very frequent to define variables on the module level. I very often see a class definition and then its instance (which I think is supposed to be used as a singleton?). Is it a bad practice and if so, why I see it so often?

Top answer
1 of 9
15
Module ≠ global. Your are correct that Module (and everything declared at that level) is singleton by definition and this is one way of implementing singletons in Python. We can debate whether the singleton pattern itself is an anti-pattern until the cows come home, but regardless of our conclusion we all recognize that they exist. (There are other way to implement singletons, BTW. Using metaclasses, for example.) More to your question, we must declare things at Module level and its not inherently bad. Recall that class definitions are themselves instances of (usually) type objects in the python object model. If we blanket say that Module level declarations are bad, then we can, effectively, never use modules at all (which is absurd). Module level declarations are also frequently used to provide aliases to things that would otherwise not be in the modules names pace, which is one way we can easily manage how users will interact with our Module (IE: define the set of vars/functions/classes that are prominent in the API for the module). What is important to note is that anything you declare at Module level is instantiated when the module IS IMPORTED not when the declared item is accessed. So, you probably don't want to put the contents of a massive database into a variable here (not that you ever really should). You also probably dont want to do any seriously intensive computation here (IE: You dont want it to take 5min just to import the module to compute something that the user might not need). But defining some lightweight things that should be exposed to all module users is fine. Constants, functions, classes and so on. This is just a matter of design: would it be better to enclose a variable as a member of a class or as a member of the module? In most cases, if it will change over time, a smaller scope than the Module is preferred even if the variable is a singleton, but exceptions can and do exist. --- Unfortunately, the above is a bit of a long winded nonanswer. To summarize, its usually preferred to put things into a smaller scope than the module, but its still acceptable to put anything at module level if that is what your design calls for. If I just have to give a rule of thumb for beginners: you can put it at module level if it is constant, a function, a class, an alias or a simple singleton. If it doesn't fit one of those, its probably worth reconsidering your design. Ofc, as you advance, you absolutely can break this so long as you can explain why you are doing it; its guidance, not a rule.
2 of 9
7
module-level vars are fine in Python for config or singletons, just avoid mutables.
🌐
Reddit
reddit.com › r/learnpython › why not use global variables?
r/learnpython on Reddit: Why not use global variables?
July 27, 2021 -

I have people telling me to avoid using global variables in my functions. But why should I?

Top answer
1 of 14
40
They add unneeded complexity and doubt into your code. Imagine you have a variable at the start of your program called "really_important" now imagine your code is 50,000 lines long and somewhere in there you import another module that also has a "really_important" global. Imagine trying to figure out which one is which, when they're used, when they're being modified, by whom, etc. Scope is a very powerful organizational tool. It helps you (and your IDE) remember what is important for any piece of code. For example: x = 0 y = 0 def add_x_y(): return x + y In the above you need to remember that this adds x and y together and inside the function you have zero assurance that x and y are even set. Contrasted with: def add_x_y(x, y): return x + y Not only is this shorter, the function prototype tells you exactly what you need to give it (two things named x and y), your IDE will helpfully provide you with insight about it, and the error you receive if you failed to define x or y properly will make a lot more sense.
2 of 14
12
Global variables are not universially bad. It is absolutely fine to use global variables for constants, values that are not changed during the execution of the program. The thing that people (rightfully) warn about is writing to or modifying the values of global variables from functions as a means of sharing state between them or writing to global variables read by functions as means of passing data to them. Ideally, a function should be self-contained: it interacts with its environment only via the arguments it was passed and the value it returns. That has two distinct advantages: The function (and the program as a whole) is less prone to errors, since each function's behaviour is determined only by the code within it and the well-defined interface it has to other code via the calling mechanism. For example, if you have multiple active calls of the same function or set of functions, either due to multithreading or recursion, functions that share state globally can easily result in unwanted, hard-to-debug behaviour. It makes the function more reusable, since it is not entangled with the state of other unrelated functions or application-specific code. Say, you realise later in your project that you need to solve a problem you have already written a function for someplace else. It is that much easier just to directly reuse the existing function if you do not have to worry about the other code that surrounds it. That said, there are legitimate uses even for non-constant global variables. The issue is that those uses are highly specific/advanced. The things that beginner or intermediate programmers tend to use global variables for are not such legitimate uses, hence the oft repeated mantra of "global variables bad" you see in this sub.
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
If a name is declared global, then all references and assignments go directly to the next-to-last scope containing the module’s global names. To rebind variables found outside of the innermost scope, the nonlocal statement can be used; if not declared nonlocal, those variables are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).
🌐
sqlpey
sqlpey.com › python › python-global-variables-across-modules
Python Global Variables Across Modules: Best Practices & Solutions
November 4, 2025 - It’s crucial to ensure the list is initialized only once, typically in the main script before other modules that depend on it are imported. ANS: The best practice for global variables in Python is to centralize them in a dedicated module (often named config or settings).
🌐
Python.org
discuss.python.org › python help
Global variables shared across modules - Python Help - Discussions on Python.org
June 25, 2022 - Hello to all Pythonians here. I encountered a strange behavior about the global keyword and modules, which I cannot understand. Module test1: Variable a is created Module test2: Module test1 is imported, and function f is created, which modifies variable a through the global keyword Module ...
🌐
Readthedocs
wemake-python-styleguide.readthedocs.io › en › 0.8.1 › pages › violations › best_practices.html
Best practices — wemake-python-styleguide 0.8.1 documentation
In Python you can specify anything in the base classes slot. In runtime this expression will be evaluated and executed. We need to prevent dirty hacks in this field. ... Use only attributes, names, and types to be your base classes. ... # Correct: class Test(module.ObjectName, MixinName, ...
🌐
Llego
llego.dev › home › blog › best practices for variable naming and scope management in python
Best Practices for Variable Naming and Scope Management in Python - llego.dev
June 6, 2023 - Declare constants in ALL_CAPS at the module level and avoid changing their value. Properly naming variables and managing scope makes Python code more readable, maintainable and less prone to bugs over time.