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

Ken Seehof kseehof at neuralintegrator.com
Sun Jan 6 21:14:48 EST 2002


Python does not have very good support for paranoid programming paradigms.
If you need support for paranoid programming, you probably need to find a
different language.  By "paranoid programming", I mean attempts to make it
impossible to write bugs.  Unfortunately, this is an impossible task, since
any language sufficiently power to implement a Turing machine is also
capable of expressing bugs.

When evaluating a language feature's ability to prevent bugs, one must ask
what kind of bug is going to be prevented and speculate how often such a bug
would occur in real life.  Immutable constants, for example, are probably
useless most of the time, since it is unlikely that an intended constant
would be accidentally changed, and anyway the resulting bug would probably
not be difficult to find.  Too much paranoia can increase the overall
complexity of your software, thereby making it intrinsically less robust.

IMHO, the best way to prevent bugs is to create a nice clean architecture,
write clean readable code, and avoid unnecessary complexity.  Finish the
program sooner so you have more time for testing.  Python has excellent
support for these techniques, so it is not surprising that python
programmers tend to spend less time debugging than those using more paranoid
languages.

That being said, there are mission critical applications where extensive
testing is difficult and a bug means somebody dies.  Python might not be
appropriate in such situations.

Ken Seehof
kseehof at neuralintegrator.com
www.neuralintegrator.com

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






More information about the Python-list mailing list