converting perl to python - simple questions.

Aahz Maruch aahz at netcom.com
Sun Apr 25 20:58:41 EDT 1999


In article <000001be8f3e$eea9c3c0$d39e2299 at tim>,
Tim Peters <tim_one at email.msn.com> wrote:
>[sweeting at neuronet.com.my]
>> ...
>> Anyway, since I know that there are a few ex-perlmongers on the list,
>> would somebody be so kind as to confirm whether I've translated
>> the following code snippets correctly :
>>
>> a) Perl's "defined".
>>    [perl]
>>    if (defined($x{$token})
>>
>>    [python]
>>    if (x.has_key(token) and x[token]!=None) :
>
>If should be enough to do
>
>    if x.has_key(token):
>
>under the probably-correct theory that the Perl is just asking "does hash
>'x' have key 'token'?"  

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.)
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Hi!  I'm a beta for SigVirus 2000!  Copy me into your .sigfile!




More information about the Python-list mailing list