Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

on "Namespaces"

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


webtourist at gmail

Nov 8, 2009, 1:20 PM

Post #1 of 4 (205 views)
Permalink
on "Namespaces"

New bie Question:
in "Zen of Python" - what exactly does the last one mean ? -
Namespaces are one honking great idea -- let's do more of those!

I mean why the emphasis ? Is it like saying "put modules into
packages" in other programming paradigm s ?

thanks
--
http://mail.python.org/mailman/listinfo/python-list


rhodri at wildebst

Nov 9, 2009, 5:47 PM

Post #2 of 4 (193 views)
Permalink
Re: on "Namespaces" [In reply to]

On Sun, 08 Nov 2009 21:20:23 -0000, webtourist <webtourist [at] gmail>
wrote:

>
> New bie Question:
> in "Zen of Python" - what exactly does the last one mean ? -
> Namespaces are one honking great idea -- let's do more of those!
>
> I mean why the emphasis ? Is it like saying "put modules into
> packages" in other programming paradigm s ?

Like all things zen, 'meaning' as applied to this koan is a shifting
concept best considered after deep meditiation on... oh, who am I kidding.

If you keep names in separate namespaces, you are less likely to screw up
by forgetting that you meant something else by that name 30 lines above.
It's less about "put modules in packages" and more about "put code in
modules."

Corollary: what happens after "from somewhere import *" is all your own
fault.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 9, 2009, 9:02 PM

Post #3 of 4 (186 views)
Permalink
Re: on "Namespaces" [In reply to]

On Sun, 08 Nov 2009 13:20:23 -0800, webtourist wrote:

> New bie Question:
> in "Zen of Python" - what exactly does the last one mean ? - Namespaces
> are one honking great idea -- let's do more of those!
>
> I mean why the emphasis ? Is it like saying "put modules into packages"
> in other programming paradigm s ?

Modules are namespaces. So are packages.

Classes and class instances are namespaces.

Even function scopes are namespaces. When you write:


n = None

def spam(n):
print "spam" * n

def ham(n):
print "ham" * n

the n inside spam() and ham() and the global n are in different
namespaces, and so independent.


http://en.wikipedia.org/wiki/Namespace
http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 10, 2009, 2:45 PM

Post #4 of 4 (180 views)
Permalink
Re: on "Namespaces" [In reply to]

Steven D'Aprano <steven [at] REMOVE> writes:

> Modules are namespaces. So are packages.
>
> Classes and class instances are namespaces.
>
> Even function scopes are namespaces.

Steven implies it with his wording, but to make it explicit:

When you have a module, package, class, or instance-of-a-class object,
those objects are themselves namespaces. That is, the name used to refer
to the object can be a component in a namespace reference::

import foo_package
import bar_module

class Parrot(object):
widget = object()

parrot = Parrot()

# Use a package as a namespace.
foo_package.spam_module.frobnicate()

# Use a module as a namespace.
bar_module.spangulate()

# Use a class as a namespace.
print Parrot.widget

# Use an arbitrary instance as a namespace.
parrot.state = "Restin'"

When you have a function object, the “function scope” is not available
in this way: you can't access the “inside” of the function from the
outside via the function object. (The function object, like any other
object, has a namespace, but that's not the *function scope* namespace.)

> When you write:
>
>
> n = None
>
> def spam(n):
> print "spam" * n
>
> def ham(n):
> print "ham" * n
>
> the n inside spam() and ham() and the global n are in different
> namespaces, and so independent.

Right. And neither of them is available from outside those functions
(since the reference only exists while the function is executing).

--
\ “Roll dice!” “Why?” “Shut up! I don't need your fucking |
`\ *input*, I need you to roll dice!” —Luke Crane, demonstrating |
_o__) his refined approach to play testing, 2009 |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.