[Tutor] question 1

Steven D'Aprano steve at pearwood.info
Sun Sep 21 06:21:46 CEST 2014


Hi Clayton, and welcome.

My responses are interleaved between your questions below.

On Sat, Sep 20, 2014 at 03:20:09PM -0700, Clayton Kirkwood wrote:
> I'm ramping slowly unfortunately. How does one go about knowing which module
> to import to make certain functions work?

Experience, practice, reading the documentation, experimentation.


> I have a read() that fails because
> there is no definition for it. I am using the Wing IDE. I have traversed
> much of the developer's guide and can't find any certainty.

That's a hard question to answer. Where did you get the read() from? I 
can think of three scenarios:

(1) You thought to yourself, "I want to read some data from somewhere, 
hmmm, this sounds like a job for a function called read(), maybe if I 
just try it, it will work!"

Sadly, no it doesn't. What sort of data are you trying to read, and from 
where? If you tell us that, we may be able to advise on the correct way 
to read.


(2) You are copying some code from somewhere else, and it includes 
something like:

    result = read(abc, xyz)  # or whatever

You need to go back through the rest of the code until you find a line 
that looks like either:

    from something import read

or perhaps:

    read = something

or even:

    def read(this, that, another):
        ...


(3) You're reading a tutorial or book, and it suggests using:

    result = read(abc, xyz)

In which case, read the tutorial a bit more closely. Perhaps earlier in 
the book they told you where read() comes from? Or maybe they didn't, 
and the tutorial is just broken, mistaken, buggy or confused.

If you show us some of the code around the read() line, we may be able 
to guess what they meant to say.


> Secondarily, why can you import a module without it importing all of its
> daughters? And why do you have to use a 'for in to import submodule', why
> not ' mport module.sub'?

A concrete example might help, because if I'm understanding you 
correctly, you *can* do exactly that.

When you say:

    import fe.fi.fo.fum

Python starts off by importing fe, then fe.fi, then fe.fi.fo, then 
fe.fi.fo.fum, so that *all* of those dotted names will work. Here's an 
actual example:

    >>> import os.path
    >>> os
    <module 'os' from '/usr/local/lib/python3.3/os.py'>
    >>> os.path
    <module 'posixpath' from '/usr/local/lib/python3.3/posixpath.py'>


So even though I only *manually* imported the os.path submodule, Python 
automatically imported its parent os for me.


> Lastly, in some tutorials and else-sourced docs certain lines have a ';' at
> the end. This seems to be most often with 'opens' and 'fopen' kind of calls.

Are you sure you're reading Python tutorials, not C tutorials? :-)

It's not *wrong* to end lines with a semi-colon, but it is poor style. A 
bit like saying "Um" at the beginning of every sentence. You would have 
to ask the tutorial author why they are doing such a thing. Do you have 
a link to an online tutorial that does that, I'm curious to see it for 
myself.


-- 
Steven


More information about the Tutor mailing list