Python Forum
global variables, classes, imports
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global variables, classes, imports
#1
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?
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
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)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Can you give a code example? Your question doesn't quite make sense.
Reply
#4
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()
Reply
#5
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(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?
Reply
#7
so how is it that modules (imports) get into global scope and assignments don't?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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).
Reply
#9
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.
Reply
#10
how does the code in a method access the global namespace?
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
  Trying to understand global variables 357mag 5 1,122 May-12-2023, 04:16 PM
Last Post: deanhystad
  Organizing several similar classes with overlapping variables 6hearts 7 1,384 May-07-2023, 02:00 PM
Last Post: 6hearts
  Global variables or local accessible caslor 4 1,029 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,150 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 947 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,596 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 1,956 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,568 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 2,560 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,826 Mar-17-2021, 11:57 AM
Last Post: cff

Forum Jump:

User Panel Messages

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