Python Namespace And Variable Scope
Python Namespace And Variable Scope

Python Namespace And Variable Scope

Try this: type ‘import this’ in the interpreter.

  1. >>> import this

Read more in detail introduction to Python for easy understanding

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren’t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one– and preferably only one –obvious way to do it.

Although that way may not be obvious at first unless you’re Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it’s a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea — let’s do more of those!

Learn Python Syntax

What is Python Name

Before we move on to namespaces in python, let’s talk about names in python. A Python name is an identifier- something we use to access a Python object and in Python, everything’s an object.

We’ll take an example.

  1. >>> rank=1

Here, ‘rank’ is the name associated with the Python object 1. To get this object’s address in RAM, we use the id() function.

  1. >>> id(rank)

492979856

  1. >>> id(1)

492979856

To take a slightly more complex example, we store 2 in a name ‘a’. Then, we increment it by 1 and associate the name ‘b’ to the object 2. We keep checking the id as we go.

  1. >>> a=2
  2. >>> id(a)

492979872

  1. >>> a+=1
  2. >>> id(a)

492979888

  1. >>> b=2
  2. >>> id(b)

492979872

  1. >>>
  2. >>> id(2)

492979872

  1. >>> id(3)

492979888

So what’s actually happening? We’ll illustrate.

Java Namespace – Python Name

As you can see, when we set ‘a’ to 3 and set ‘b’ to 2, ‘b’ starts pointing to the object ‘a’ once pointed to. Isn’t that quite efficient? It does not have to create another object to hold 2 for b. This dynamic name binding is powerful.

Also, a name can hold any kind of value.

  1. >>> a=1
  2. >>> a='one'

Finally, since everything is an object, so are Python functions. Consequently, you can associate them with names.

  1. >>> identity=id
  2. >>> identity(2)

492979872

Here, we associate the name ‘identity’ with the built-in function id().

Bonus Question- Check the following code and figure out what’s happening.

  1. >>> hi=sayhello()

Hello

  1. >>> hi
  2. >>> type(hi)

<class ‘NoneType’>

Well, since the function does not return anything, we get an object of class ‘NoneType’. Of course, None is an object that indicates no value. Did function sayhello() return a value, things would be different. Let’s take another example.

  1. >>> def func1():
  2. print("Hi")
  3. return 1
  4. >>> func2=func1()

Hi

  1. >>> func2

1

  1. >>> type(func2)

<class ‘int’>

Introduction to Python Namespaces

A namespace in python is a collection of names. So, a namespace is essentially a mapping of names to corresponding objects. At any instant, different python namespaces can coexist completely isolated- the isolation ensures that there are no name collisions. Simply speaking, two namespaces in python can have the same name without facing any problem. A namespace is implemented as a Python dictionary.

When we start the interpreter, a python namespace is created for as long as we don’t exist. This holds all built-in names. It is due to this that python functions like print() and id() are always available. Also, each module creates its own global namespace in python.

When you call a function, a local python namespace is created for all the names in it. A module has a global namespace. The built-in namespace encloses this. Take a look at the following figure to get a clearer understanding.

Python Variable Scope

Through various python namespaces, not each can be accessed from every part of the program. A namespace is in variable scope in a part of a program, if it lets you access the python namespace without having to use a prefix.

At any instant, we have at least three nested python scopes:

  1. Current function’s variable scope- has local names
  2. Module’s variable scope- has global names
  3. The outermost variable scope- has built-in names

This in accordance with the three kinds of namespaces in python, we just discussed. This also decides the order of searching for when a reference is made. The order is- the local Python namespace, the global namespace, the built-in namespace. Also, a nested function creates a nested variable scope inside the outer function’s scope.

Top 10 Best Python Book for Beginners & Experienced -2018

Read Complete Article>>

要查看或添加评论,请登录

社区洞察

其他会员也浏览了