No Thoughts about Everything

Dave Brueck dave at pythonapocrypha.com
Thu Feb 26 10:32:48 EST 2004


bblochl wrote:
> I found a solution for the simulation of access modifier "static final "
> in python:
>
>
> class Mytest:
>     LOGE_10=(2.302585092994046,)
>     #Klassenkonstante (static final) use a tuple :-)
>     #def __init__(self):
>
>     def log10(self,x):
>         return(log(x)/LOGE_10[0])
>
> testmath=Mytest()
> print "LOGE_10=%10.8f"%Mytest.LOGE_10[0]," and
> log10=%f10.8"%testmath.log10(3)
> Mytest.LOGE_10[0]=1
> #print "LOGE_10=%10.8f"%Mytest.LOGE_10[0]," and
> log10=%f10.8"%testmath.log10(3)

You want constants because you're worried that somebody will reassign the
value, but do you (1) actually expect that that same programmer will remember
to index it and (2) won't reassign a list value? :) Forcing the programmer to
remember to do LOGE_10[0] instead of just LOGE_10 will be a source of way more
bugs.

Nothing is stopping him/her from doing:
    Mytest.LOGE_10 = 5
  or
    Mytest.LOGE_10 = [5]

If you really have to deal with this type of programmer, Python is probably not
a good language to use. I recommend Pascal or something more strict because
Python does little or nothing to protect against ill-intentioned /
undisciplined programmers.

But whatever you choose to do, please please PLEASE don't teach this to your
new Python programmers - it is not how Python programs are written, it makes
the code less readable, and it doesn't solve any real problem (where "real"
means "one that actually occurs in practice").

If it gives you some sense of security, then go ahead and use it in your own
private code, but please don't pass this along to your students as "how you
simulate Java's  'final' modifier in Python" because that's incorrect.

Nice talking to you - have fun!
-Dave





More information about the Python-list mailing list