Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global varname
#1
i have many (~100) functions in a big file. most functions need to write a single global variable named varname. i can make this work by putting global varname in each function that needs to write it. is there a simple way to do this in one place and have it affect the entire file (every function defined in this file)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Can do it like this.
# sitecustomize.py
import builtins

builtins.varname = 100
Save sitecustomize.py in Path of Python that you use,eg root folder.
Now in any function without import will varname work as a builtin-in.
def somefunc(number: int) -> list[int]:
    lst = []
    lst.append(number * varname)
    lst.append(number * 4)
    return lst

print(somefunc(5))
Output:
[500, 20]
Reply
#3
what happens if one of these function has import math? should or could i just include import math in sitecustomize.py?

many of the functions will be adding to, or subtracting from, varname. i need to be sure they can do that. some will do that through calls to functions i cannot modify. otherwise i would put its value in a 1-list and access it with a [0] index.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I would create an object to hold the global variable in an attribute
glo = type('glo', (), {})()
glo.varname = 0

def foo():
    glo.varname += 7
    ...

def spam():
    glo.varname *= 2


if __name__ == '__main__':
    foo(); spam(); foo()
    print(glo.varname) # prints 21
You can even sophisticate this technique to alter actual global variables
class Glo:
    def __init__(self, dic):
        self.__dict__ = dic
glo = Glo(globals())

varname = 0

def foo():
    glo.varname += 7
    ...

def spam():
    glo.varname *= 2


if __name__ == '__main__':
    foo(); spam(); foo()
    print(varname) # prints 21
    
Reply
#5
(Mar-02-2023, 05:10 AM)Skaperen Wrote: what happens if one of these function has import math? should or could i just include import math in sitecustomize.py?
Yes can go crazy🚀
# sitecustomize.py
import builtins
from math import cos, tan

def super_tan(tan):
    return tan ** 2

builtins.varname = 100
builtins.cos = cos
builtins.super_tan = super_tan
Now will this code magically work,no import.
If someone look at this code they may wonder what 🤯 is going on.
So if only you use this it can be ok,or can dokument this behaviour.
Can be a design problem in bottom🧐
def func(number):
    result = cos(number) + super_tan(number) / varname
    return result

print(func(5))
Output:
0.5336621854632262
Reply
#6
This is so weird. Just a day or two ago there was another thread wondering if there was a way to clump together imports so they are all done at once. I am so averse to changing anything in builtins that it didn't occure to me that adding import objects to builtins would do exactly what he wanted. That still doesn't stop it from being a REALLY BAD IDEA.

file imports.py
import math, builtins  # As long as I am doing bad things
builtins.math = math
file test.py
import imports  # Only need to do this once.  Now math is in builtins
print(math.cos(math.pi))
Output:
-1
This works. After math gets added to builtins, other files don't even have to import "imports". If I set this up as a package, I could pollute the builtins namespace inside an __init__.py file and make it even harder to track down when math was promoted to a builtin. This not only creates confusion when anyone reads your code, it also hamstrings nice programming tools like autocomplete, snippets and kite. pylance hates the code: 'imprts' imported but not used, 'math' is not defined.
Skaperen likes this post
Reply
#7
i can see where this is bad code, worse if someone else might ever need to diagnose it. but for one person who can remember it, or if good documentation is included, it might be a usable "hack".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variable does not seem to be global. Columbo 6 3,908 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

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