Please send questions to
st10@humboldt.edu .
480 Python Projected Miscellany
Lect 13, 11-15-05
Beginning of in-lecture exercise:
1. What would you NOW type at the >>> prompt below to see
lect13_1's spam's value of 40?
(after the
>>> from lect13_1 import spam
)
2. What would you NOW type at the >>> prompt below to see
lect13_1's spam's value of 40?
(after typing, in a new python session,
>>> import lect13_1
)
* recall: built-in function named dir
returns a LIST of the attributes of the argument object
>>> import lect13_1
>>> dir(lect13_1)
* a name that begins and ends with two underscores
(e.g. __name__, __doc__)
...is a system-defined name in Python.
(ref: Section 2.3.2, Python Reference Manual,
docs/python/org/ref/ref.html, follow the link to 2.3.2)
* these are names defined by the interpreter and its
implementation (including the standard library);
* "applications should not expect ot define additional
names using this convention"
* __doc__ is set to a string (if you've put one) that is the
FIRST thing in a module, a def, a class, etc. --- that is then
stored in the __doc__ attribute of the corresponding object.
(such a string is called a docstring...)
* the PyDoc tool makes for "nicer" reports using docstrings
and other info...
ONE way to use it: is with the built-in help function
help(object) --- invokes PyDoc to generate a UNIX-man-page-like
textual report on that object
* LOOKS like Python MAY be lacking in the kind of true
information-hiding capabilities you may be familiar with
in C++, Java, Ada, etc.;
BUT there ARE ways to discourage users from seeing/mucking with
SOME things;
* 1st way: IN a module, if you begin a name with a single
underscore, it isn't copied out when a user uses from *
(it IS imported using import, however)
* hmm... help(that_module) doesn't include such names,
either...
* intended to "minimize namespace pollution"... [Learning
Python, p. 279, Ch. 18]
* 2nd way: define a list named __all__ in the module;
...then ONLY those names in the __all__ list will be
copied over using from *.
(STILL get 'em all with import...)
* NOTE: Python looks for an __all__ list 1st --- if
it finds it (when from* has been done), only the
names in that list are copied out.
OTHERWISE, from* copies out all but the _blah names
* (yes, it behaves oddly, IMHO, with help and __all__...)
* __name__ and __main__
...lets a module be conveniently used for command-line or import use
* point in fact: when you import a module, __name__ for that module
is set to be the module's name as known by the client
* BUT --- if a file is run as a "top-level" program file --- __name__
is set to the string '__main__'
* then: intro to OBJECTS (but didn't add to this projected file for
that; see lect13_7.py, lect13_8.py for examples used to spur
discussion.)