On Tuesday 30 May 2006 11:13, Talin wrote:
> For example - suppose I have two dictionaries that I wish to
> search, such that if I don't find a value in the first, I want to look
> in the second; And if I don't find a value in the second, I want to
> default to zero. (This pattern happens to me rather a lot - because I
> tend to do a lot of stuff with parsing and nested symbol tables.)
>
> One simple but inefficient way to code this would be:
>
> value = a.getitem( key, b.getitem( key, 0 ) )
This reminds me of one feature of the last scripting language I designed prior
to Python taking over my world - an exception handler operator which could be
used in expressions.
The ?? operator first evaluated its left operand. If that succeeds its value
is returned. If that raised an exception it evaluates and returns its right
operand. That allowed your example to be written:
value = a[key] ?? b[key] ?? 0
That was handy for the scripts which this language was designed to support: It
was a common requirement that values had to be found by scavenging through
increasingly more desperate sources of information. All of the sources were
fallible, and some default value needed to be substituted in case all other
methods failed.
--
Toby Dickenson
____________________
Important Notice:
This email and any attachments are confidential and may contain trade secrets
or be legally privileged. If you have received this email in error you must not
use, rely upon, disclose, copy or distribute the contents. Please reply to the
sender so that proper delivery can be arranged and delete the email from your
computer.
Gemini Data Loggers monitor incoming and outgoing email to ensure satisfactory
customer service, maintain company security and prevent abuse of their email
system. However, any views expressed in this email are not necessarily those of
Gemini and Gemini cannot be held responsible for the content.
Gemini makes best efforts to ensure emails are virus free; however you are
advised to carry out your own checks. Gemini does not accept responsibility for
any damage resulting from email viruses.
____________________
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com