Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why need namespace in python #3749

Closed
jacquesqiao opened this issue Aug 30, 2017 · 1 comment
Closed

why need namespace in python #3749

jacquesqiao opened this issue Aug 30, 2017 · 1 comment

Comments

@jacquesqiao
Copy link
Member

jacquesqiao commented Aug 30, 2017

why need namespace in Python

Conclusion

  1. Python function should not have a Block.
  2. Should have namespaces for name isolation.

Reasoning

Why Cannot Each Python Function Has a Block

If every function in python is a new Block. Then in the code below, function D() call G() inside, G() return a Variable inside the g_scope,
it's hard to get variable from g_scope in d_scope.

There are several ways to do it:

  1. we can use a pointer to collect all scopes and find inside them, but there will be many variables with the same name inside various scopes, it will be difficult to find the right variable.

  2. we can copy the x in g_scope to scope when calling G() in D(), and then get it from the scope. this will import many variable copies but the copying is very expensive because some var may be very big.

with scope():
	def G():
		with g_scope(scope):
			x = Var("x")
			reutrn x
		
	def D():
		with d_scope(scope):
			x = Var("x")
			y = x + G()
			return y

Namespace as a Solution

so we need a namespace in python to do name isolate.

Variable will have namespace as the prefix

  1. the name of variable x inside G will be global/G/x
  2. the name of variable x inside D will be global/D/x

all the variables are put inside on scope, so they can always find each other if the namespace and name is right.

with namespace("global"):
	def G():
		with namespace("G"):
			x = Var("x")
			reutrn x
		
	def D():
		with namespace("D"):
			x = Var("x")
			y = x + G()
			return y
@jacquesqiao
Copy link
Member Author

we have name guard now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant