?

Log in

No account? Create an account

Previous Entry | Next Entry

programming challenge

I’ve had a hard time coming up with programs to write to practice using Python. So I thought I’d take a crack at last year’s ICFP programming contest challenge. That way I’d not only get to try out Python but also get an idea of the kind of task that might be assigned, or at least the scale of it.

HA HA HA HA HA. Very funny, self. I went through the task description, churning out code fragments here and there, mostly for the world simulation in which our actual submissions would run. A couple hours and many google searches (“what’s the syntax for such-and-such again?”) later, I gave up. This exercise would be suitable if I had several days of free time and a foundation of Python experience. This exercise would not be suitable as the first program I write in a new language.

Question for you Python coders: what would be a good way to simulate a typedef in Python? Like for a simple enumerated type where I don’t care what the values are but I want to define a type for readability and to take advantage of Python’s being strongly typed?

Comments

( 3 comments — Leave a comment )
uninja
Feb. 22nd, 2005 04:22 pm (UTC)
Python is dynamically typed (in addition to being strongly typed), so you don't explicitly declare types as in languages like Java or C++. The closest you can come to that is declaring a class, but that seems like overkill when a tuple, list, set, dictionary, or some combination will likely be adequate for data. For a simple enumeration you can do things like:

A, B, C, D = range(4)

There's also a handy Enumeration class (<40 lines) in the Python Cookbook that is substantially more powerful. I hope that helps.
radhardened
Feb. 22nd, 2005 06:26 pm (UTC)
I'll have to check out the Enumeration class. I think I prefer the overkill of a class to A, B, C, D = range(4) for the type safety. I just went and ordered Learning Python, Python Pocket Reference 3rd Edition (coming out this month), and Python Cookbook 2nd Edition (coming out next month).
(Anonymous)
Feb. 22nd, 2005 05:56 pm (UTC)
ambition
Hehe, I read the task for this project. It sounds amazing... and complex (code generating code). But for an introduction to Python, maybe it's not the best choice. :)

--Brian
( 3 comments — Leave a comment )