What is the concept of namespace when importing a function from another module?

2024/7/8 8:29:35

main.py:

from module1 import some_function
x=10
some_function()

module1.py:

def some_function():print str(x)

When I execute the main.py, it gives an error in the moduel1.py indicating that x is not available.

My understanding was that using from x import y in module main.py brings the definition/value of x.y in the local namespace of main.py. And since both the function definition and variable x are in local namespace of main.py, it should work ok. But this seems incorrct undersyanding. So what is the exact concept here? Any link to officcial python documentation for this concept?

Answer

Each module has its own global name space.

from module1 import some_function
x=10
some_function()

some_function uses module1.x in its definition, but you are setting x in the current module. This would work:

from module1 import some_function
import module1module1.x = 10
some_function()

Note that you can't use from module1 import x, then set x = 10, because that import simply initializes a new name x to have the same initial value as module1.x; x = 10 then gives a new value to the new variable.

https://en.xdnf.cn/q/119931.html

Related Q&A

How to pass a literal value to a kedro node? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.This po…

How to Loop a List and Extract required data (Beautiful Soup)

I need help in looping a list and extracting the src links. This is my list and the code: getimages = getDetails.find_all(img) #deleting the first image in the list getimages[0].decompose() print(getim…

square root without pre-defined function in python

How can one find the square root of a number without using any pre-defined functions in python?I need the main logic of how a square root of a program works. In general math we will do it using HCF bu…

How do I sort a text file by three columns with a specific order to those columns in Python?

How do I sort a text file by three columns with a specific order to those columns in Python?My text_file is in the following format with whitespaces between columns:Team_Name Team_Mascot Team_Color Te…

regular expression to search only one-digit number

Im trying to find sentences having only one digit number along with.sentence="Im 30 years old." print(re.match("[0-9]", sentence)then it returns<re.Match object; span=(0, 1), mat…

Automate adding new column and field names to all csv files in directories [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Connect the python app to a database using centos 7

I am new to all this I have apython app already helo.mysql.py and need to Connect the python app to a database. I am using centos 7 and have it installed on a ec2 instance if anyone can help please he…

How do I restart my program in Python? (see code)

if option == C:radius = float(raw_input("Enter Radius: ")) area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))elif option == T:base = …

how to create a list of elements from an XML file in python

my XML <root> - <Book category="Children"><title>Harry Potter</title> <author>J.K</author> <year>2005</year> <price>29.99</price> &…

Is random.sample truly random?

I have a list with 155k files. When I random.sample(list, 100), while the results are not the same from the previous sample, they look similar. Is there a better alternative to random.sample that retur…