Newbie Q: Class Privacy (or lack of)

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 27 04:00:21 EDT 2006


Steve Jobless wrote:
> Sybren Stuvel wrote:
> 
>>Steve Jobless enlightened us with:
>>
>>>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 :).
>>
>>Then learn to read what you type, as you type it. Typing without
>>errors can be trained.
> 
> 
> I'd rather let a machine to do that. Wasn't computer created for tasks
> like this? (No, not really. But...)

There's obviously a trade-off between 'security' and flexibility. As I
said, I do make lots of typo too, but OTOH the edit/test cycle in Python
is usually so short that such errors are not a problem for me - they're
caught almost immediatly.

> 
>>>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.
>>
>>You shouldn't simply pick a name and assign something to it without
>>checking the current use of that name. It's pretty much true for
>>everything in life.
> 
> 
> Well, the choice of "next" was not a good example. Sure, no one with
> decent Python knowledge would do that.
> But what about adding a method to the class? Am I supposed to ask "Is
> anyone using name xxx?"

assert 'xxx' not in dir(SomeClassOrObject)

> The class may be used by  developers I don't
> even know and some of them may be on the other side of the planet...

How could this be a problem ? What's added/changed at runtime in a given
app doesn't impact the source code.

> 
>>>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.
>>
>>Long live a versioning system. With that, you can find the person
>>writing the horrible code, and slap them on the back of the head.
>>People, like all animals, can be trained into doing the right thing.
> 
> 
> I'd like to. But often those people are long gone for one reason or
> another. Sometimes, it happens to be my boss...

If your boss is a bad programmer and doesn't know it, then you're in for
trouble whatever the language.

> 
> Maybe I should ask the question in a different way:
> 
> What are the benefits from this? There must be something really good
> (and hopefully safe) you can do with it only from the outside. I don't
> have much problem over-riding functions from the inside of the class.
> But from the outside, I fail to see why anyone needs to add attributes
> or over-ride functions.

Just one example : suppose you're working with a framework. Suppose
you'd need to customize some objects (including classes - Python classes
are objects too) of the framework, but the framework has no hook for
this and you definitively don't wan't to bother patching the code and
maintaining your own branch.


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