Python Forum
global variables, classes, imports - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: global variables, classes, imports (/thread-2077.html)

Pages: 1 2


global variables, classes, imports - Skaperen - Feb-17-2017

i set up a module with a class.   outside the class i import a module and define a variable.  in a method of the class in can use that module or object that was imported but not that variable that was assigned a value.  ok, i understand there are no globals in classes.  but then, how do the modules work in this situation?  what if i do from module_name import * and assign a variable in that module?


RE: global variables, classes, imports - wavic - Feb-17-2017

You can pass arguments to the class.

class Star(object):
    def __init__(self, size, mass)
        self.size = size
        self.mass = mass

earth_mass = 5.9722*(10**24)
the_sun = Star(695700, 330000*earth_mass)



RE: global variables, classes, imports - micseydel - Feb-17-2017

Can you give a code example? Your question doesn't quite make sense.


RE: global variables, classes, imports - Larz60+ - Feb-17-2017

Correct me if I am wrong, but I think your saying that you want to something like the following:
from tkinter import *

class Notepad:
    def __init__(self):
        self.root = Tk()

def main():
    np = Notepad()
    master = np.root
    master.mainloop()

if __name__ == '__main__':
    main()



RE: global variables, classes, imports - Skaperen - Feb-18-2017

(Feb-17-2017, 06:11 PM)Larz60+ Wrote: Correct me if I am wrong, but I think your saying that you want to something like the following:
from tkinter import *

class Notepad:
    def __init__(self):
        self.root = Tk()

def main():
    np = Notepad()
    master = np.root
    master.mainloop()

if __name__ == '__main__':
    main()

so in this case how does any method in class Notepad find Tk ... or what if you do:

from tkinter import *
alt_Tk = Tk


class Notepad:
    def __init__(self):
        self.root = alt_Tk()

def main():
    np = Notepad()
    master = np.root
    master.mainloop()

if __name__ == '__main__':
    main()
in my pipelines class (not done, yet) i had a bunch of initial values assigned before the methods (and after the imports) but the assigned values did not show up even though the imports did.


RE: global variables, classes, imports - micseydel - Feb-18-2017

(Feb-18-2017, 04:20 AM)Skaperen Wrote: so in this case how does any method in class Notepad find Tk ... or what if you do:
It was imported into the global scope. I don't see the problem. What do you gain with the indirection you're doing?


RE: global variables, classes, imports - Skaperen - Feb-18-2017

so how is it that modules (imports) get into global scope and assignments don't?


RE: global variables, classes, imports - micseydel - Feb-18-2017

They do if you do them in the global scope. And similarly, if you import somewhere other than the global scope, you import into that scope instead (star imports are disallowed anywhere other than global scope, but this holds any other time).


RE: global variables, classes, imports - Larz60+ - Feb-18-2017

from an old effbot blog here

Quote:from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.



RE: global variables, classes, imports - Skaperen - Feb-19-2017

how does the code in a method access the global namespace?