Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Namespace
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=====Python===== In [[Python (programming language)|Python]], namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then namespaces are hierarchical too.<ref>{{cite web|title=6. Modules|url=https://docs.python.org/tutorial/modules.html|work=The Python Tutorial|publisher=Python Software Foundation|access-date=25 October 2010}}</ref><ref>{{cite web|url=https://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces |title=Python Scopes and Namespaces |publisher=Docs.python.org |access-date=2011-07-26}}</ref> In general when a module is imported then the names defined in the module are defined via that module's namespace, and are accessed in from the calling modules by using the fully qualified name. <syntaxhighlight lang="python"> # assume modulea defines two functions : func1() and func2() and one class : Class1 import Modulea Modulea.func1() Modulea.func2() a = Modulea.Class1() </syntaxhighlight> The <code>from ... import ...</code> statement can be used to insert the relevant names directly into the calling module's namespace, and those names can be accessed from the calling module without the qualified name: <syntaxhighlight lang="python"> # assume Modulea defines two functions : func1() and func2() and one class : Class1 from Modulea import func1 func1() func2() # this will fail as an undefined name, as will the full name Modulea.func2() a = Class1() # this will fail as an undefined name, as will the full name Modulea.Class1() </syntaxhighlight> Since this directly imports names (without qualification) it can overwrite existing names with no warnings. A special form of the statement is <code>from ... import *</code> which imports all names defined in the named package directly in the calling module's namespace. Use of this form of import, although supported within the language, is generally discouraged as it pollutes the namespace of the calling module and will cause already defined names to be overwritten in the case of name clashes.<ref>https://docs.python.org/3/tutorial/modules.html "in general the practice of importing * from a module or package is frowned upon"</ref> Python also supports <code>import x as y</code> as a way of providing an alias or alternative name for use by the calling module: <syntaxhighlight lang="numpy"> import numpy as np a = np.arange(1000) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)