[Tutor] Slightly OT: public/private class members

Gonçalo Rodrigues op73418@mail.telepac.pt
Fri Feb 14 10:32:02 2003


----- Original Message -----
From: "Jerry Jorgenson" <jerry@j3iss.com>
To: <tutor@python.org>
Sent: Friday, February 14, 2003 2:49 PM
Subject: Re: [Tutor] Slightly OT: public/private class members


> > And what does strict (Perlicism?) do?
>
> strict enforces declaration of variables before usage. So
>
> use strict;
>
> makes you issue a
>
> my $variableName;
>
> statement so the if you accidentally type $myVar or $myvariable later on,
> an error will occur, rather than a new variable being created. This is
> great for code maintainability, especially when you cut and paste
functions
> from other code.
>

That's what I thought. In Python >= 2.2 there is a similar mechanism but for
*instance attributes*: __slots__.

>>> class test(object):
...  __slots__ = ['myattribute']
...  def __init__(self, attribute):
...   self.myattribute = attribute
...
>>> a = test(8)
>>> a.myattribute
8
>>> a.x = 1
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: 'test' object has no attribute 'x'


> Jerry
>

With my best regards,
G. Rodrigues