Newbie Q: Class Privacy (or lack of)

Ben Sizer kylotan at gmail.com
Thu Jul 27 09:53:07 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...)

One of the benefits of Python is being able to dynamically add
attributes like that, or indeed to be able to access attributes that
haven't explicitly been created. If it raised an error each time you
tried to add something outside of an __init__ function, there are many
Python tricks you could no longer achieve. Unfortunately, when you come
from a language like C++ or Java where you learned to live without
those benefits, often not even knowing such things existed, all you see
are the negatives (ie. lack of checking).

--
Ben Sizer





The


>
> >
> > > 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?" 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...
>
> >
> > > 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...
>
>
> 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.
> 
> SJ




More information about the Python-list mailing list