converting perl to python - simple questions.
Florian Weimer
fw at cygnus.stuttgart.netsurf.de
Tue Apr 27 15:10:42 EDT 1999
andrew-johnson at home.com (Andrew Johnson) writes:
> Just a point of clarification: Actually, perl programmer's
> traditionally use 'exists' to test if a key is in a hash ...
No, unfortunately that's wrong:
| defined(EXPR)
|
| defined EXPR
| Returns a boolean value saying whether the lvalue
| EXPR has a real value or not. Many operations
| return the undefined value under exceptional con-
| ditions, such as end of file, uninitialized vari-
| able, system error and such. This function allows
| you to distinguish between an undefined null
| string and a defined null string with operations
| that might return a real null string, in particu-
| lar referencing elements of an array. You may
| also check to see if arrays or subroutines exist.
| Use on predefined variables is not guaranteed to
| produce intuitive results. Examples:
|
| print if defined $switch{'D'};
| [...]
(Quotation from the Perl 4 manpage. This version doesn't have an
`exists' function.)
> using
> 'defined' to test for key existence is a mistake---'defined' will
> only tell you if that key exists and has a defined value associated
> with it: Witness the defined test on key2 in the following:
Of course you are right. That's the reason why `exists' was introduced
-- I guess in Perl 5.
More information about the Python-list
mailing list