Are rank noobs tolerated, here?
Jeffrey Froman
jeffrey at fro.man
Tue May 6 10:24:08 EDT 2008
notbob wrote:
> I'm running
> vers 2.5.1 on slackware 12.
Nice to see another Slackware user around here!
> "Here is an example of a user-defined function that has a parameter:
>
>
> def print_twice(bruce):
> print bruce, bruce
<snip>
> is this just an example of how the def should be written and it doesn't
> really do anthing... yet?
That's correct. A function doesn't generally *do* anything until it is
called. Here, it is only defined. The only thing this function does when
called is to print the value of bruce twice.
> I define myfirstfunction in the pyth
> editor and give the command print myfirstfuntion and I get back this:
> <function myfirstfunction at 0xb7b9c994>
Functions are objects too, and this is a printed representation of that
function object. It still hasn't been "called" at this point.
> ....when I add the ._doc_, it get this:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'function' object has no attribute '_doc_'
> ....so, there is a no go there, too.
The functions docstring is stored in the attribute named "__doc__". Like
other python "special" attributes, it starts and ends with TWO underscores.
> ok, I try and follow the above, but where is he getting the script? So, I
> make a script called chap03.py and put it in ~/pyth/chap03/.
You are correct to create this script yourself from scratch.
> I then try an do the import thing above, but the pyth ed accepts none of
> it. not from:
> from ~/pyth/chap03/ import *
> from chap03 import * #taken from ~/pyth/
> ....nor from any other configuration I can imagine, so that "from" command
> makes no sense.
Modules are imported from your PYTHONPATH, which is a search path similar to
bash's PATH: It is a list of directories that are searched for *module*
names when you attempt an import. You can examine your python path by
doing:
>>> import sys
>>> sys.path
Your module files, i.e., chap03.py, should be in one of the directories on
that path.
"from" modifies a *module* name, not a path component. So "from module
import one_function, another_function". This allows you to use one_function
without referencing it through the imported module name. In other words,
these two are equivalent:
>>> import chap03
>>> chap03.print_twice()
and:
>>> from chap03 import print_twice
>>> print_twice()
In the above examples, "chap03" is the *module* file, chap03.py.
Hope that helps clarifies things a bit,
Jeffrey
More information about the Python-list
mailing list