confused if i should be doing type checking in the code ( a confused java programmer)

Karthik Gurumurthy karthikg at
Sun Jan 6 08:24:38 CET 2002

hi all,

have been doing python for a couple of months now and have been doing java
for more than a year now.

had trouble implementing immutable constants in python

i wanted a public static "final" equivalent in python.

class MovieRating:
	NEW = 2

Now even though i can access it like

But someone can modify the value

MovieRating.REGULAR = 100 # UnAcceptable

I was trying to emulate the java enum equivalent but was not able
implement it successfully.

class MovieRating{

 public static final MovieRating REGULAR = new MovieRating(1)
 public static final MovieRating NEW = new MovieRating(2)
 public static final MovieRating CHILDREN = new MovieRating(3)

 private int value;

 private MovieRating(int i){
  value = i;

Now the client cannot instantiate MovieRating but can get all the constant
so there's a proper type checking in another code which accepts only
MovieRating instances
instead of just integers.

So in python code i needed a singleton and then the immutable constants.
Then came across this recipie in Cookbook and Well it's brillaint!
i had'nt even imagined such things could be done.

I used that code and wrote this:

class _MovieRating:
	def __init__(self):
		self.__dict__['REGULAR'] = 1
		self.__dict__['NEW'] = 2
		self.__dict__['CHILDREN'] = 3
	def __setattr__(self,name,value):
		if self.__dict__.has_key(name):
			raise "Cannot assign to a readonly movie rating %s" % (name,)
			raise "Cannot add new constants"

import sys
sys.modules[__name__] = _Movie()

So in java if there's a functon which accepts the movie ratings,

void func(MovieRating rating,<something else){


We have been taught to use enum pattern in java to store constants instead
of primtives like
ints or strings because we get the benefit of type checking in the compile
stage itself and no one
can send in a wrong rating value.

Since this kind of a things cannot be done in python ,should i be writing
this kind of code in python as well??

def func(rating,x)
	if type(rating) != MovieRating:
		raise 'You have passed an Incorrect type'

if i do that then all my python functions and methods will be littered with
such type checking code!

even after a couple of months of python..i think i have not been able to
understand the differences in the way
you code in java and python!  :-(
probably am missing something here..the purpose of the 2 languages appear to
be very different to me now.

i took a look at the __slots__ concept as well which i guess does not allow
us to add attributes to an instance.
I saw a recipe for writing properties. That was smart!
But now python supports properties at syntax level. I tried that out with

Then what prevents python from having a final equivalent when we could have
such nice things like properties?

thanks for your patience!

-----Original Message-----
From: tutor-admin at [mailto:tutor-admin at]On Behalf Of
Kirk D Bailey
Sent: Sunday, January 06, 2002 10:42 AM
To: tutor at
Subject: [Tutor]

To whoever feels like it;
I have got almost complete. There is one minor bug in
there. Regretfully, it's a tiny little stinkbug and it is driving me

First, take a look at the script as it stands this very instant- page
inherently self updates.

Second, there is no error code, but it fails to see the presence of
the subject line in the pending file.

Here is the screen capture from a sample run:
(beware wordwrap)
ns# ./ < testfile
From:= highprimate at
Subject field as extracted:='Re: join highprimate at
evil-humor 1010287984'
CLEANED UP subject line='join highprimate at evil-humor
oldfrom='highprimate at'
Pending action='join testcode at testlist3 1010173860'
This person wants to join'evil-humor' list!
cannot find subject in pending file!

I included a LOT of print statements in the program to generate all
these status reports. The language's interpeter is not creating ANY

I am stumped for the moment, and tired, and battling with a cranky
isp, so I think I will smoke a house pipe and drink some fine coniac
and go night night, but feel free to look at this, and write me some
questions, answers, suggestions, pithy things to double check, evil
jokes, etc...

So close... SO close...


In total confusion,
                   Kirk D Bailey

 |   Providing Excellent email service for free!!     |
 |    Webmaster, Howlermonkey Email services Co.      |
 | highprimate at |


Refill your inkjet cartridges. Same top quality print. Low Low price. <a
href="">Click Here.</a>
(They have a nice affiliate program too!)

Tutor maillist  -  Tutor at

More information about the Python-list mailing list