Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Difference between import and from in Python
Python's "import" loads a Python module into its own namespace, so that you have to add the module name followed by a dot in front of references to any names from the imported module that you refer to:

import feathers
duster = feathers.ostrich("South Africa")


"from" loads a Python module into the current namespace, so that you can refer to it without the need to mention the module name again:

from feathers import *
duster = ostrich("South Africa")


or

from feathers import ostrich
duster = ostrich("South Africa")


Q Why are both import and from provided? Can't I always use from?

A If you were to load a lot of modules using from, you would find sooner or later that there was a conflict of names; from is fine for a small program but if it was used throughout a big program, you would hit problems from time to time

Q Should I always use import then?

A No ... use import most of the time, but use from is you want to refer to the members of a module many, many times in the calling code; that way, you save youself having to write "feather." (in our example) time after time, but yet you don't end up with a cluttered namespace. You could describe this approach as being the best of both worlds.

(written 2005-08-18 00:11:06)

 
Associated topics are indexed under
Y105 - Python - Functions, Modules and Packages

Back to
Telephone Preference Service - we're registered
Previous and next
or
Horse's mouth home
Forward to
Most popular courses

Some other Articles
PHP Magic Quotes
Don't repeat code - use loops or functions
Towards Tebay
Most popular courses
Difference between import and from in Python
Telephone Preference Service - we're registered
Mixing up swallows and martins
Campaign Drift and efficiency.
Form Madness
Save the train
1636 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho