sharing global variables among several py files?

the situation is:
i like to use global variables and global functions instead of putting them inside
class App(DirectObject):
, which requires me to write self. in front of most names.

so now i have, for example:

a=1
def func():
    global a
    a+=1
    return

when my py script grow larger, i want to put func() in another file.
but func() writes to global variable a.
then how can i put func() in another py file?

a related problem: can i put functions which are defined inside
class App(DirectObject):
into another file ?

for example:

a=1
class App(DirectObject):
    def __init__(self):
        b=1

    def func1(self):
        global a
        a+=1
        return

It is generally not considered good practice to rely too much on global variables. Store them in classes, and pass instances of those classes around in your class hierarchy.

But you could simply define globals in one file, import that file, and then use the globals from there. Just keep in mind that some things only work if you import your other file like “import otherfile” and then do “otherfile.x = 3”, though if you use “from otherfile import *” you are copying the globals into the namespace of this file and any assignment of “x = 3” will only change it in the current namespace.

Thank you.
that makes sense now. my attempt of sharing global variables among several py files failed.

one more question:

i found that if v1=v2 and v2 is a list, v1 is in fact an instance of v2 and they change together.

for example:

import otherfile
otherfile.v2=[1,2]
v1=otherfile.v2

then if i change either one of v1 or v2, the other changes also.

but if v2 is a number, such as

otherfile.v2=1
v1=otherfile.v2

then v1 don’t change with v2, or vise-versa.

is it possible to make v1 an instance of otherfile.v2 when v2 is a number?

Primitive types like integers are immutable, so this is not possible.