Forwarding response to tutor; didn't realize it was only sent to me.
---------- Forwarded message ---------- From: Danny Yoo <d...@hashcollision.org> Date: Sun, Sep 21, 2014 at 3:10 PM Subject: Re: [Tutor] question 1 To: Clayton Kirkwood <c...@godblessthe.us> On Sun, Sep 21, 2014 at 2:42 PM, Clayton Kirkwood <c...@godblessthe.us> wrote: >> Secondarily, why can you import a module without it importing all of >> its daughters? > > The act of importing a module is "recursive": if you import a module, and > that module itself has import statements, then Python will do the import of > the child modules too. And so forth. >> And why do you have to use a ‘for in to import submodule’, why not >> ’import module.sub’? > > If I'm not mistaken, you _can_ do this. Can you point us to a source where > it says you can't? > > (There are particular style guidelines from certain organizations that > prohibit this kind of import, but that prohibition is by convention. > That is, it's not because the language disallows it, but because it doesn't > fit with the house coding style.) > > > [Clayton Kirkwood] > From the interpreter: > > import io > import io.FileIO > Traceback (most recent call last): > File "<string>", line 1, in <fragment> > builtins.ImportError: No module named 'io.FileIO'; 'io' is not a package Hi Clayton, Ah. But 'FileIO' is not a module. It's a value in the 'io' module. We can see this here: ####################################################### dhcp-9:work dyoo$ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import io >>> io <module 'io' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.pyc'> >>> io.FileIO <type '_io.FileIO'> ####################################################### When we say: import xxx then the "xxx" has to be a module. The 'from/import' statement is deceptively named, unfortunately; it's doing a little more than just module import. https://docs.python.org/2/tutorial/modules.html#more-on-modules where when we say: from xxx import yyy then xxx.yyy could be a module, or it might not. The act of module import is involved, so in a sense, the statement is doing an import. But the 'yyy' doesn't have to be a module. When we say: from io import FileIO we are taking a value from the 'io' module, and giving it the name 'FileIO So yeah, it's a little confusing. For that reason, certain style guidelines have a say when something is appropriate to write, apart from what Python technically allows. As a concrete example, see: https://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Imports#Imports Here, we see a style decision that is _more_ restrictive than what the Python language allows. > Huh. Can you point to a reference? Note that third-party documentation (and > official documentation!) might be buggy or weird. > :P If you can point us to an example, maybe one of us can investigate what's > going on there. > [Clayton Kirkwood] > > > Example from ' http://www.tutorialspoint.com/python/python_files_io.htm' > > #!/usr/bin/python > > str = raw_input("Enter your input: "); > print "Received input is : ", str Just because something is on the web (or email) doesn't mean it's correct or prefect. :) -- There are a few small problems with this example from tutorialspoint: 1. The semicolon is superfluous. Take it out: it will still work. 2. The name 'str' is already a built-in, https://docs.python.org/2/library/functions.html#str so the code here is shadowing the built-in. In such a small test program, this is fine. In a real program, I'd ding this for readability issues. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor