declaration of variables?

Brian Quinlan brian at sweetapp.com
Sun Feb 16 16:31:22 EST 2003


 > And I do think that declaring a variable isn't that much of a hassle,
> atleast not of the magnitude you (and others) make it out to be. 
> Chasing down "strange" errors is.

Declaring variables in Python would severely reduce its dynamic nature.
The complete set of variables need might not be known at compile-time,
so how could they be declared? As an example, imagine that I write a RPC
client that must be able to receive proxies to object instances. How
would I know what variables that object has at compile-time? I also
might want to do something wacky like write a module that exposes
environment variables as globals (like PHP does). How would I know what
variables I need a compile-time?

> Hmm, how about this example: if a given function sorts some list, if 
> there is an error in there that doesn't cause a run-time error. How is

> it possible to detect this without having to write the whole routine 
> once more and compare the results? (which, of course, is an 
> error-prone process).
> 

I'd test a simple integer sort function like this:

assert sort([]) == []
assert sort([1]) == [1]
assert sort([0]) == [0]
assert sort([-1]) == [-1]
assert sort([1, 2]) == [1, 2]
assert sort([2, 1]) == [1, 2]

numbers_sets = [range(-100, 100, 20), 
		    range(-100, 120, 20),
		    range(-9, 12, 3)]

for numbers in number_sets:
	for test in permute(numbers):
		assert sort(test) == numbers

assertRaises(sort, ['hello'], TypeError)
assertRaises(sort, [10000L], TypeError)
assertRaises(sort, range(100) + ['hello'], TypeError)
assertRaises(sort, [1.5], TypeError)
assertRaises(sort, range(100) + [1.5], TypeError)

Keep in mind that I only spent a few minutes no this and this test
should be supplemented with whitebox knowledge. 

Cheers,
Brian






More information about the Python-list mailing list