[Tutor] using class methods (security)

Paul Sidorsky paulsid@shaw.ca
Tue, 23 Apr 2002 15:29:51 -0600


Paul Tremblay wrote:

> It somehow seems wrong to access a class variable by using an
> object, though I guess this second method should work, and seems
> more secure and preferable to the first.

>         def numOfCircles(self):
>                 print Circle.__counter

I was about to recommend simply removing "self" from the parameter list
and calling this as Circle.numOfCircles() when I discovered it doesn't
work!  It seems like such a natural solution that I very nearly didn't
even bother to verify it.

Naturally I got curious about why it doesn't work so I went to find the
answer.  Luckily I didn't have to look too far:  there's an explanation
in the FAQ, question 6.24, along with some recommendations.

I think it's kind of a pity this doesn't work in the "obvious" way.  On
the other hand, I've never found class variables and methods to be all
that useful so it doesn't bother me that they aren't supported all that
well.  I expect one could research the debate in the c.l.py archives if
one wanted to, though.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/