Newbie Q: Class Privacy (or lack of)

Bruno Desthuilliers onurb at xiludom.gro
Tue Jul 25 03:18:08 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.

Actually this is __ultra_private. For normal privacy, _private is enough !-)

> 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.

Obviously. But why would you do such a stupid thing ?

OTOH, this let you customize func() for a given object or class. Which
can be very helpful sometimes.

> Are these bugs or features?

Features, definitively.

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

My experience is that with medium/large projects, this feature can
actually help to solve a lot of problems in a much more simpler and
straightforward way than with most mainstream languages.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list