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

Karthik Gurumurthy karthikg at aztec.soft.net
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:
	REGULAR = 1
	NEW = 2
	CHILDREN = 3

Now even though i can access it like

MovieRating.REGULAR
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
instances.
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.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207

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,)
		else:
			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.

anyways,
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
py2.2

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

thanks for your patience!
karthik.




















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


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

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

http://www.howlermonkey.net/TLlistmaster.shtml

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# ./TLlistmaster.py < testfile
From:= highprimate at howlermonkey.net
Subject field as extracted:='Re: join highprimate at howlermonkey.net
evil-humor 1010287984'
CLEANED UP subject line='join highprimate at howlermonkey.net evil-humor
1010287984'
command='join'
oldfrom='highprimate at howlermonkey.net'
listname='evil-humor'
Pending action='join testcode at howlermonkey.net testlist3 1010173860'
This person wants to join'evil-humor' list!
cannot find subject in pending file!
ns#

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

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...












end

In total confusion,
                   Kirk D Bailey

 +----------------------------------------------------+
 |   Providing Excellent email service for free!!     |
 |    Webmaster, Howlermonkey Email services Co.      |
 | highprimate at howlermonkey.net www.howlermonkey.net/ |
 +----------------------------------------------------+

ODD#1.8.01/kdb/sigme


Refill your inkjet cartridges. Same top quality print. Low Low price. <a
href="http://www.inkwellkits.com/cgi-bin/at.pl?a=147030">Click Here.</a>
(They have a nice affiliate program too!)


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor




More information about the Python-list mailing list