Re: [Tutor] question 1

2014-09-20 Thread Danny Yoo
On Sat, Sep 20, 2014 at 3:20 PM, Clayton Kirkwood c...@godblessthe.us wrote:
 I’m ramping slowly unfortunately. How does one go about knowing which module
 to import to make certain functions work? I have a read() that fails because
 there is no definition for it.

Specific details may help here.  Can you tell us more?  What are you
trying to do?  If you are seeing error messages, please copy exact
error message content and present it.  Error messages have more than
one bit of information: they usually encode some information that
folks here can try to interpret.


 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.

(You might imagine a system where the main module must do all this
linkage by itself, rather than do this recursive walk.  And there are
systems that do work like this!  See:
https://talks.golang.org/2012/splash.article for Rob Pike's discussion
on the Plan 9 compiler, which worked this way.

But Python does not do this.)



 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.)


 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.

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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question 1

2014-09-20 Thread Danny Yoo
 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.

Hi Deb,

Oh!  By the way, this echoes that point about recursion from a week or so ago.

https://mail.python.org/pipermail/tutor/2014-September/102715.html

So yes, recursion shows up at the heart of the Python module import
system too.  That should have been an obvious example, but I forgot.
:P
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question 1

2014-09-20 Thread Steven D'Aprano
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor