[PEP 224] Attribute Docstrings
M.-A. Lemburg
mal at lemburg.com
Mon Aug 28 07:59:37 EDT 2000
Lawrence Kesteloot wrote:
>
> > Here is an example:
> >
> > class C:
> > "class C doc-string"
> >
> > a = 1
> > "attribute C.a doc-string (1)"
> >
> > b = 2
> > "attribute C.b doc-string (2)"
>
> Correct me if I'm wrong about this, but initialization of things
> like lists can't be done in a class definition. For example:
>
> class C:
> x = []
>
> wouldn't work because the list itself would be shared among
> the instances (assuming you don't want that). This kind of
> initialization must be done in the constructor in order to
> create a new list for each instance.
That's true. I usually add a declaration of the following
form to classes:
class C:
x = None
" List of names "
def __init__(self):
# init instance vars
x = []
Doing this assures that the attribute x is always defined and
is also good practice since it declares the attributes in the
class rather than hiding them in the constructor.
> If that's the case,
> then this PEP is only useful for non-mutable attributes
> such as floats, strings, and integers.
>
> Is this correct? Or would you just do:
>
> class C:
> x = []
> "attribute C.x does whatever"
>
> def __init__(self):
> self.x = list(self.x)
>
> which seems bad.
>
> Either way this PEP seems like it has limited value.
>
> Personally, I would love to declare all class attributes like you
> do instead of in the constructor. But Python doesn't seem to
> work that way as far as I know.
>
> Lawrence
--
Marc-Andre Lemburg
______________________________________________________________________
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
More information about the Python-list
mailing list