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)
#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


Messages In This Thread
RE: global namespace of an imported function (2 Qs) - by Skaperen - Oct-09-2018, 12:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can a module tell where it is being imported from? stevendaprano 3 1,209 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  Function global not readable by 'main' fmr300 1 1,370 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,701 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  'namespace' shorthand for function arguments? shadowphile 5 2,644 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  [newbie] Why is a module imported twice? Winfried 3 4,108 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  Finding global extrema of oscillating function JoeRogan 0 1,670 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
Star NameError – function doesn't recognize imported modules Sir 4 3,555 Dec-01-2020, 06:36 AM
Last Post: Sir
  How to make global list inside function CHANKC 6 3,138 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,250 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,467 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