Newbie Q: Class Privacy (or lack of)

John Machin sjmachin at lexicon.net
Mon Jul 24 23:28:05 EDT 2006


Steve Jobless wrote:
> Hi,
>
> I just started learning Python. I went through most of the tutorial at
> python.org. But I noticed something weird. I'm not talking about the
> __private hack.
>
> Let's say the class is defined as:
>
>   class MyClass:
>     def __init__(self):
>       pass
>     def func(self):
>       return 123
>
> But from the outside of the class my interpreter let me do:
>
>   x = MyClass()
>   x.instance_var_not_defined_in_the_class = 456

Uh-huh. Could result from a typo. Found in testing. pychecker and/or
pylint may help here. Use __slots__ if this bothers you.

>
> or even:
>
>   x.func = 789
>
> After "x.func = 789", the function is totally shot.

So is the developer :-)

>
> Are these bugs or features? If they are features, don't they create
> problems as the project gets larger?
>

Features. They don't *create* problems by themselves. If a project
already has  problems (like they hire idiots, and don't have
appropriate review and testing), then yeah things can blow up because
of some language features being mis-used -- this applies to any
language. Further, there are lots more reasons why projects blow up,
few of them related to a couple of features of the development
language.

Another way of looking at it: languages can't tell who's using them. If
they constrain idiots, they also constrain non-idiots.

Cheers,
John




More information about the Python-list mailing list