Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Namespace
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Namespace
(section)
Add topic