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

Hans Nowak wurmy at earthlink.net
Sun Jan 6 17:10:47 EST 2002


Karthik Gurumurthy wrote:

> had trouble implementing immutable constants in python

That's because Python doesn't have such a beast. As you
already found out, you can more or less fake them, though.

> 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

First question: Why would someone want to do that?

I have a few programs where users can enter pure Python code.
In some cases, it may be desirable to protect people from
unwittingly overwriting certain things, so then it might be
a good idea to have some protection mechanism. But if the
code is just part of a program that is being executed,
then I usually won't bother protecting these attributes. It
goes against Python's concepts of dynamism, and besides,
if someone is really determined to change that value, then
they will, protection or not.

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

If you must. But going against a language's nature is guaranteed
to give you lots of trouble and headaches. See below...

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

Yes, they have very different philosophies. Python is dynamic.
I personally see forced typechecking, like in the 'func' function
above, as a restriction of Python's capabilities. Why would you
want to restrict a function (argument) to accept only one
specific type? Python happily allows you to use objects that
"behave" in the same way as the object/type you intended. 

As a crude example, consider:

    def twice(x):
        return x + x

This function seems obviously made to take a numeric value,
be it an integer or a float (or a long, or a complex...).
But due to Python's dynamism, it works for any object that
supports the + operator in a meaningful way. This includes
strings:

>>> twice("c")
'cc'

...lists:

>>> twice([1,2])
[1, 2, 1, 2]

...and any object that defines the __add__ method. Now this
is a trivial example, but normally, this feature of Python
is a nice thing to have for your functions, and it greatly
encourages code reuse (although probably in a different
way than hardcore OO zealots would want you to use ;-).

If you do pass the "wrong" type (that is, something that
does not support the + operator), you get an exception. So
there is some type-checking done... just not at the place
where Java & friends do it.

But, the language does allow you to be paranoid if you
want; you can do things like

    def add(x, y):
        assert type(x) == types.IntType
        assert type(y) == types.IntType
        return x + y

but this is a lot of extra typing, and probably not worth
it. Well, you decide.

> 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

Well, if you want to make Python behave like C++ or Java,
then use them. If you want Python to behave like Python, ...

Regards,

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list