Newbie Q: Class Privacy (or lack of)
Steve Jobless
sjobless at rotten.apple.commie
Wed Jul 26 00:54:03 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
>
> or even:
>
> x.func = 789
>
> After "x.func = 789", the function is totally shot.
>
> Are these bugs or features? If they are features, don't they create
> problems as the project gets larger?
>
> TIA,
>
> SJ
Thanks, everyone. I'm hearing that "they are features, but don't use them."
It's not that I'm going to do things like them intentionally.
The first case can be just a typo, like:
x.valeu = 5
I make typos all the time. Without a spell checker, this message would
be unreadable :).
The second case can be like:
x.next = y
y.next = None
to create a linked list by piggybacking "next" to the class. It will
overwrite the iterater for the class if defined.
If I was working on a large project with many engineers, I'd assume
someone will do things like this sooner or later. I've seen many
horrendous code in my life and I have no control over who I work with.
SJ
More information about the Python-list
mailing list