Tutoral < ? < Language Reference
Jim Dennis
jimd at vega.starshine.org
Fri Apr 5 14:05:09 EST 2002
In article <3281a460.0204050705.4b9b595f at posting.google.com>, P Adhia wrote:
> Hi,
> I think I have graduated to the next level after reading the tutorial.
> But I find language reference just describing the formal grammer,
> probably meant for programmers who already know python well (rather
> too well). Just as an example, by readking lang. reference I still
> don't know what "..." is and couldn't find refences to __all__,
> __slots__. Is there a "python referece" available somewhere, which
> covers every aspects of python, possibly with explanation+examples
> (something akin to what K&R is for C) ?
>Thanks
>P. Adhia
... is an Ellipsis; I gather that there is such a thing as an
Ellipsis object which I guess is some way of representing
recursive references. For example:
a = []; a.append(a)
... If you do this from an interactive interpreter session
and then just type:
>>> a
... you should get:
[[...]]
(a list containing a list containing an Ellipsis object)?
I won't pretend to understand this; and maybe I'm completely
mistaken. However, this is the impression I've gotten. As you
say, the Tutorials (and other books and docs I've see) don't
explain it.
__all__ is a member of a package which lists "all" of the
classes that are intended to be part of the package (exposed via
the dir() built-in). It's mostly used in the sorts of modules
that are implemented as a directory tree, so it would be set
in an __init__.py file.
__slots__ is a special member for 2.2 "New" classes (as opposed
to "classic classes"). It provides support for constraining the
list of attributes for an object. Basically it means that efforts
to add new attributes in an object will raise an AttributeError
exception. (Unless they are in __slots__ or if the __setattr__
method of an object handles it for us?).
"""
Finally, it's possible to constrain the list of attributes that can be
referenced on an object using the new __slots__ class
attribute. Python objects are usually very dynamic; at any time it's
possible to define a new attribute on an instance by just
doing obj.new_attr=1. This is flexible and convenient, but this
flexibility can also lead to bugs, as when you meant to write
obj.template = 'a' but made a typo and wrote obj.templtae by accident.
A new-style class can define a class attribute named __slots__ to
constrain the list of legal attribute names.
"""
You can read much more about these and other 2.2 features at:
http://www.amk.ca/python/2.2/
More information about the Python-list
mailing list