Python Forum

Full Version: global variables, classes, imports
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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)
Can you give a code example? Your question doesn't quite make sense.
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()
(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.
(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?
so how is it that modules (imports) get into global scope and assignments don't?
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).
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.
how does the code in a method access the global namespace?
Pages: 1 2