[Tutor] confused if i should be doing type checking in the co de
( a confused java programmer)
alan.gauld@bt.com
alan.gauld@bt.com
Sun, 6 Jan 2002 20:28:46 -0000
Hi Karthik,
> have been doing python for a couple of months now and have
> been doing java for more than a year now.
You have my sympathy, Java is a truly dreadful language.
> had trouble implementing immutable constants in python
Yep, you can do it by playing tricks with classes to implement singletons
etc but its not really necessary.
Bjarne Strtoustrup used to say when Smalltalkers
criticised C++ : "If you want to program in Smalltalk
then use Smalltalk not C++"
Every language has its own idioms and Python is a dynamic
language wheras Java is a static, strictly types language.
There are lots of things you can do in Python that you
can't do in Java and vice versa, thats always the way
with programming languages. Don't get hung up on
translating every single feature, learn the strong points
of each language and use them to best effect.
Accept the weaknesses, you'll get less grey hair that way.
> i wanted a public static "final" equivalent in python.
Why exactly? Are you so lacking in discipline that you
won't observe a naming convention say?
If so adopt a functional style of programming, that will
save you from making any such mistakes...
Or is it maybe just that you are used to that level of hand
holding from Java?
> class MovieRating:
> REGULAR = 1
> NEW = 2
> CHILDREN = 3
Why not use a dictionary? Unless you are also writing methods
to manipulate movie ratings a dictionary would be a more natural way to
express this:
MovieRating = {'REGULAR':1, 'NEW':2,'CHILDREN':3)
Now access via: MovieRating['Regular']
You can still change it but its a more Pythonic approach IMHO.
> MovieRating.REGULAR = 100 # UnAcceptable
Yes, but if you comment (or better document) the fact that
you shouldn't change uppercased values(a fairly common
convention) then why would anyone sanely ever do so?
(OK I know they might not read the docs! But programming
by "contract" or convention is not that unusual)
> Now the client cannot instantiate MovieRating but can get all
> the constant instances.
Which aren't really instances at all nor is the MovieRating
really a class in the OO sense, thats what I meant earlier
about Java being a bad language. It encourages programmers
to use classes to do all sorts of really BAD things to
the Object Model.
> Then came across this recipie in Cookbook and Well it's brillaint!
> i had'nt even imagined such things could be done.
Yes thats the clever stuff with classes. Its open and extensible,
its Python(and Lisp and Smalltalk and Perl and...) It also
opens the way to lots of things that are really difficult
in Java or C++. And with the new support for metaclasses and classmethods in
PythonV2.2 things get even more interesting.
> 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.
Says more about Javas compiler (and the stupid distinction
between primitives and classes) than anything else!
> 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'
In the few cases when you do need to check types yes,
but usually its better to write generic functions that
can accept almost any type then catch the exceptions with:
try: #doit here
except TypeError: # oops! that wont work
> understand the differences in the way
> you code in java and python! :-(
Don't worry about it, it always takes a wee while.
But also don't try to do too literal a translation, understand
that each will have its own stengths and weaknesses.
Alan g.
(Who writes in Python for fun and profit and in Java
only when my customer insists!)