concept namespace in category python

appears as: Namespaces, A namespace, namespace, Namespaces, namespace, namespaces
The Well-Grounded Python Developer MEAP V03

This is an excerpt from Manning's book The Well-Grounded Python Developer MEAP V03.

What you've done is to create namespaces, each one narrowing the scope of what it contains. Reading the path from left to right each segment of the path separated by the / character creates a new, narrower namespace within the context of the previous one.

Practices of the Python Pro

This is an excerpt from Manning's book Practices of the Python Pro.

Like many programming languages, Python isolates code through the concept of namespaces. As a program runs, it keeps track of all the known namespaces and the information available in those namespaces.

Namespaces are helpful in a few ways:

  • As software grows, multiple concepts will need similar or identical names. Namespaces help minimize collisions so it remains clear to which concept a name refers.
  • As software grows, it becomes exponentially more difficult to know what code is already present in the codebase. Namespaces help you make educated guesses about where code might live, if it does exist.
  • Figure 2.1. The specificity of namespaces

    You might have seen a NameError: name 'my_var' is not defined error sometime in your adventures with Python. That means the name my_var wasn’t found in any of the namespaces known to that code. This usually means you never assigned my_var a value, or you assigned it somewhere else and need to import it.

    In cases of ambiguity, Python uses the most recent definition it knows about. If you import time from one place and then import another time from another place, it will only know about the latter. If you don’t make use of namespaces, it will be difficult to tell which time() is being referenced in the code, and you might use the wrong one by mistake. This is a compelling reason to import modules as a whole; it forces you to prefix names from the module so that it’s clear where the names come from.

    >>> import this
    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!
    Hello World! Third Edition

    This is an excerpt from Manning's book Hello World! Third Edition.

    In your class, there’s only one Shawn, so when you say “Shawn,” your classmates know which person you’re talking about. To put this another way, in the space of your class, there’s only one name “Shawn.” Your class is your namespace, and in that namespace, there’s only one Shawn, so there’s no confusion.

    Now, if the principal has to call Shawn to the office over the public address system, she can’t just say, “Would Shawn please come to the office.” If she did that, both Shawns would show up at the office. For the principal using the public address system, the namespace is the whole school. That means everyone in the school is listening for the name, not just one class. So she has to be more specific about which Shawn she means. She would have to say something like, “Would Shawn from Mr. Morton’s class please come to the office.”

    The other way the principal could get the correct Shawn is to go to the doorway of your class and say, “Shawn, would you please come with me.” There would be only one Shawn listening, and she would get the right one. In that case, the namespace would be just one classroom, not the whole school.

    Importing a module means the same thing as importing a namespace. When you import the module, you import the namespace.

    There are two ways to import a namespace (or module). You can do it like this:

    import stephen_leacock

    If you do it that way, stephen_leacock is still a separate namespace. You have access to the namespace, but you have to specify which namespace you want before you use it. So the principal would have to do something like this:

    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest