class methods vs. functions

Jacek Generowicz jacek.generowicz at cern.ch
Fri Jul 16 09:16:09 CEST 2004


Egbert Bouwman <egbert.list at hccnet.nl> writes:

> On Thu, Jul 15, 2004 at 09:40:53AM -0700, Steven Bethard wrote:
> > 
> > So if you decide that your data attributes need to change, you haven't
> > destroyed compatibility with users of your class, even if your data
> > attributes were publicly available.  Continuing to support the older
> > attributes isn't necessarily trivial, but at least it's /possible/
> > (unlike C++ or Java).
> 
> Yes, I see that in real life access to the internal state of an
> object is sometimes necessary.

Let's just think about this statement. In real life, access to the
internal state of an object is _always_ necessary. What would be the
point of the internal state, if there were no means of accessing it ? 

(Yes, I'm being pedantic, but we are talking about programming
computers, and computers are pedantic in the extreme ... so you'd
better be used to expressing yourself accurately :-)

> And Python allows it.

So does C++:

    class A {
    public:
      int i;
    };
    
    A a;
    a.i;

OK, this goes against C++ dogma, so you might prefer this:

    class A {
    private:
      int i;
    public:
      int getx() { return this->i; }
      void seti(int i) { this->i = i; }
    };
    
    A a;
    a.i;

You're still accessing the internal state. And it doesn't have to be
that trivial:

    class complex {
    private:
      double r;
      double i;
    public:
      double  get_modulus() {
        return sqrt(i*i + r*r);
      }
      ...
    };


    C c;
    c.get_modulus();

You're still accessing internal state. The whole point of internal
state is that it be accessed somehow; if you couldn't access it, it
would be a waste of memory. Now, you might wish to debate _how_ it
should or should not be acessed ...

> But what should i think of systems in which the users actually
> have to use that licence ?

Maybe you want to refine your question.

As I said elsewhere ... you should probably think about the point of
interfaces (others have addressed this point upthread); you should
understand the concept of data abstraction. If you understand that,
you should be able to judge for yourself what is a good idea and what
is not. Dogmas are for people who can't think for themseleves.

I guess I could summarize my message thus:

    Don't just repeat words; understand the concepts behind the words.



More information about the Python-list mailing list