converting perl to python - simple questions.

Tim Peters tim_one at email.msn.com
Sun Apr 25 22:26:03 EDT 1999


[about Perl "if (defined($x{$token})"]

[Tim sez "under the probably-correct theory that the Perl is just
 asking "does hash 'x' have key 'token'?"]

[Aahz Maruch]
> Try "definitely wrong theory" ;-).  In Perl, exists($x{$token}) is
> precisely equivalent to Pyton's x.has_key(token), and you can either use
> defined($x{$token}) or $x{$token}!=undef to make sure that a value
> exists for that key.  Thing is, in Perl you can go straight to checking
> the value because a non-existant key is not an error.
>
> (I won't quite call myself a Perl expert, but I'm pretty close to an
> expert on Perl hashes.)

But most Perl programmers aren't, and I've suffered thru enormous piles of
Perl code that used "defined" where "exists" was appropriate.  That's where
my "probably" comes from:  not talking about what the language does, but
what the programmer probably *intended*.  In the absence of more information
about what the original script does, my guess is still that the author
*intended* "exists" == has_key.

not-all-perl-code-is-well-written<wink>-ly y'rs  - tim







More information about the Python-list mailing list