Python Forum
what is the purpose of the global name space?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is the purpose of the global name space?
#1
what is the purpose of the global name space since i can't exchange data there between various functions that could be called?
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
(Oct-15-2018, 05:37 AM)Skaperen Wrote: can't exchange data there between various functions that could be called?
Why can't you exchange data?
Reply
#3
It's not that you can't exchange data, it's that it's a bad idea to modify global values locally.

And you need a top level namespace for the names of functions and classes, at the very least.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-15-2018, 09:56 AM)Gribouillis Wrote:
(Oct-15-2018, 05:37 AM)Skaperen Wrote: can't exchange data there between various functions that could be called?
Why can't you exchange data?
each function is running with a different global space. function abc() is called and puts a number in the global space it sees under the name "foo" then returns. function xyz() is called and looks in the global space it sees for "foo" but it is not there. this is because each gets a different global space.

(Oct-15-2018, 01:30 PM)ichabod801 Wrote: It's not that you can't exchange data, it's that it's a bad idea to modify global values locally.
so... is that why they make it so you can't?

(Oct-15-2018, 01:30 PM)ichabod801 Wrote: And you need a top level namespace for the names of functions and classes, at the very least.
no you don't. they can work in the local space, too. i know because i have done it. you can even have them in a list.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
What does different global space mean? Isn't it just one where all objects are seen to any other?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
I don't know what you're talking about, but this is how the global namespace works:

CONSTANT = 5           # <---- CONSTANT in the global name space

def foo():             # <---- foo in the global name space
    return CONSTANT    # <---- retrieved from the global name space.

def bar(x):            # <---- bar in the global name space, x in the local function name space
    global CONSTANT    # <---- refers assignments to the global name space
    CONSTANT += x      # <---- modifies the global CONSTANT with the local x

if __name__ == '__main__':
    bar(3)
    print(CONSTANT)                 # <---- prints 8
    print(globals()['CONSTANT'])    # <---- prints 8
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Oct-15-2018, 08:42 PM)wavic Wrote: What does different global space mean? Isn't it just one where all objects are seen to any other?
it means not the same global space. for 2 functions there are 3 separate global spaces. i tested this with python3.5.2 under ubuntu linux. put some data in one of them and it is not in the other.

(Oct-16-2018, 01:20 AM)ichabod801 Wrote: I don't know what you're talking about, but this is how the global namespace works:
try it by importing the 2 functions separately from files foo.py and bar.py.

...
from foo import foo
from bar import bar
...
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
(Oct-16-2018, 06:59 AM)Skaperen Wrote: i tested this with python3.5.2 under ubuntu linux. put some data in one of them and it is not in the other.
Can you post this code?
Reply
#9
(Oct-16-2018, 06:59 AM)Skaperen Wrote: try it by importing the 2 functions separately from files foo.py and bar.py.

The functions don't have different global namespaces, the modules do. Two functions in the same module have the same global namespace, two functions in different modules don't. And you can access the different namespaces through dot notation (module.constant or module.function). See section 9.2 of the python 3 tutorial.

(Oct-16-2018, 06:59 AM)Skaperen Wrote: it means not the same global space. for 2 functions there are 3 separate global spaces. i tested this with python3.5.2 under ubuntu linux. put some data in one of them and it is not in the other.

If your question has to do with importing, why didn't you mention that in the first two posts of your thread? If you have code relevant to the question, why haven't you put it into any of the three posts you've made to this thread so far? It smells like trolling to me. You've already been called out for click bait titles. You ask more questions on this board than anyone else. You should know by now to put all the information relevant to the question in the first post. If you continue to fail to do so, I will start issuing warnings for trolling.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(Oct-16-2018, 09:59 AM)Gribouillis Wrote:
(Oct-16-2018, 06:59 AM)Skaperen Wrote: i tested this with python3.5.2 under ubuntu linux. put some data in one of them and it is not in the other.
Can you post this code?
maybe!

there are three files.

ien.py
def ien():
    global c
    global d
    c = 3
    d = 4
    return
twa.py
def twa():

    try: print('a =',repr(a))
    except: print('a mislearre')

    try: print('b =',repr(b))
    except: print('b mislearre')

    try: print('c =',repr(c))
    except: print('c mislearre')

    try: print('d =',repr(d))
    except: print('d mislearre')

    return
haad.py
a = 1
b = 2

from ien import ien
from twa import twa

def main():
    print('begjin')
    ien()
    twa()
    print('dien')
    return

main()
python3 haad.py
python2 haad.py


(Oct-16-2018, 01:42 PM)ichabod801 Wrote: The functions don't have different global namespaces, the modules do. Two functions in the same module have the same global namespace, two functions in different modules don't. And you can access the different namespaces through dot notation (module.constant or module.function). See section 9.2 of the python 3 tutorial.

(Oct-16-2018, 01:42 PM)ichabod801 Wrote: If your question has to do with importing, why didn't you mention that in the first two posts of your thread? If you have code relevant to the question, why haven't you put it into any of the three posts you've made to this thread so far? It smells like trolling to me. You've already been called out for click bait titles. You ask more questions on this board than anyone else. You should know by now to put all the information relevant to the question in the first post. If you continue to fail to do so, I will start issuing warnings for trolling.

i did not initially know (when i started the thread) that importing was the distinction because in all the systems i have seen before, there never was a distinction. but i did figure it out. so now it does make sense to revise my question. but i will wait because i could, again, be falsely accused of trolling.

i really am trying to learn the basic philology of Python. yes, that can make for some strange questions. it seems to be quite different than all other languages i have encountered, including those i did not spend a lot of time with because early on i could tell i did not want to code in those, such as perl and go. i have seen very little info online of the philology of Python even though i would think that its history would yield more than typical.

i normally try to keep my questions brief. that is because people have gotten mad at me for making posts that are too long. so i have tried to provide information when it is needed,so i don't end up posting things that are not needed.

i was trying to design a program with pluggable modules as a means to select different features and modes. the various plugins, which are different modules, need to exchange data between each other. one case involved other modules that needed to customize a translate table in the primary module. the changes didn't happen. eventually, i abandoned direct access to the table and created an API by having a few function or method calls. the names of the functions and classes could not be found between the modules. it now appears that even an API approach still cannot get past the isolation between separately imported modules. can packages somehow resolve this? i am not sure i can make these separate files appear to be all in the same package.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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