declaration of variables?
Mel Wilson
mwilson at the-wire.com
Mon Feb 17 09:18:11 EST 2003
In article <7xd6lrnw14.fsf at ruckus.brouhaha.com>,
Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:
>Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:
>> For the language's target environment (at least originally;
>> Python is changing from a common snake into a dragon in terms of
>> usage <G>), not having to declare variables is a positive
>> feature... a library routine could return an error code (numeric) or
>> error message (string) and the user doesn't have to know which in
>> advance.
>
>That's not so hot for the library: the caller then has to figure out
>whether the thing that came back is an error code. Better for the library
>to raise an exception on an error condition. That's why exceptions
>were invented after all.
My favorite poster child for not declaring things is:
def number (s):
"convert something (probably a string) to a convenient numeric type"
try:
return int (s)
except ValueError:
try:
return long (s)
except ValueError:
return float (s)
If I had to declare things, I'd have to declare the
return type of `number`, and obviously I don't want to.
Since the parameter `s` has no declared type, `number` is
safe to call with an object that's already a number, and it
can be called successfully with any class instance that has
a __int__, __long__ or __float__ method,
Regards. Mel.
More information about the Python-list
mailing list