Python Forum
global namespace of an imported function (2 Qs)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global namespace of an imported function (2 Qs)
#1
the global namespace of an imported function seems to be separate/distinct from the global namespace of the caller. so when is the function's global namespace created? when it is imported or when the function is first called? is there a way for imported code to access variables from the global namespace of what is importing it at import time?
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
The namespace is created when it is imported, and I believe only when it is first imported. Any top-level code in the module is executed at that time, and can access anything defined above it in that namespace.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-08-2018, 02:07 AM)ichabod801 Wrote: The namespace is created when it is imported, and I believe only when it is first imported. Any top-level code in the module is executed at that time, and can access anything defined above it in that namespace.
and what all does that include?

i would like to set a variable of a specific name followed by importing a module. i want have that module access that variable (it would know the name) and use that value to do different things like defining different functions.
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
Why would you want to do all that? Why not just pass the variable to a function in the module?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
in the one case i have first, i want the variable to help the module decide which code to define the function with. the is not the actual code, just a fun example.
if yesdoit:
    def doit():
        print('yes')
        return 'yes'
else:
    def what():
        return
the code above is the typical uselessness you might see in school classes. it's just to show the concept's codability.

i think i have found a solution: set an environment variable.

i have done this definition of a function under conditional logic before to define functions differently based which version of python is running. this allowed me to work with the unicode type in python2 and the bytes type in python3. i call this split definition.
if str == bytes:
    def prthing(x):
        if isinstance(x,(str,unicode)):
            print x
            return
        elif isinstance(x,bytearray):
            print str(x)
            return
        else:
            print repr(x)
            return
else:
    def prthing(x):
        if isinstance(x,str):
            return print(x)
        elif isinstance(x,(bytes,bytearray)):
            return print(''.join([chr(c) for c in x]))
        else:
            return print(repr(x))
but i do have different kinds of needs for split definitions. now i have come to where it is a setting by the code doing the importing.
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
  Can a module tell where it is being imported from? stevendaprano 3 1,168 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  Function global not readable by 'main' fmr300 1 1,335 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,669 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  'namespace' shorthand for function arguments? shadowphile 5 2,585 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  [newbie] Why is a module imported twice? Winfried 3 4,075 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  Finding global extrema of oscillating function JoeRogan 0 1,645 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
Star NameError – function doesn't recognize imported modules Sir 4 3,484 Dec-01-2020, 06:36 AM
Last Post: Sir
  How to make global list inside function CHANKC 6 3,081 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,206 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,417 Aug-28-2020, 09:24 AM
Last Post: Winfried

Forum Jump:

User Panel Messages

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