On 12 August 2010 17:01, Jürgen Krämer <jottka...@googlemail.com> wrote:
>
> Hi,
>
> Ingo Karkat wrote:
>> On 12-Aug-2010 10:19, winterTTr wrote:
>>>
>>> Or is there an alternate method to accessing dictionary  when the key
>>> is not found
>>> , instead of showing error directly?
>>
>> You can use
>>     echo get(foo, 'var', MyFunction('var'))
>>
>> MyFunction() can then return whatever you want if the key 'var' is not found 
>> in
>> foo. The only downside is that MyFunction() is always evaluated, even if the 
>> key
>> _is_ found.
>
>  echo has_key(foo, 'var') ? foo.var : MyFunction('var')
>
> or
>
>  echo has_key(foo, 'var') ? foo{'var'} : MyFunction('var')
>
> will execute MyFunction() only if 'var' is not found.
>
> Regards,
> Jürgen

OK, thanks so much for your answers.  I do not notice the get() method.
There is indeed an alternate way to do so. That's great.

However, I must use this code everywhere if i want the feature as i mentioned.
Even the other person who want to use my code should do the same thing.
This is not a elegant way, right?

I just want to figure out whether we can hack the dictionary object
to give it the __index__ feature at the "script" level?
I tried but i can't find a way to  map the accessing of a dictionary
to an __index__-like function call
with an _elegant_ way.

Maybe there is no way besides changing on the "source code" level.
But that's a changing to language itself  :-(

>
> --
> Sometimes I think the surest sign that intelligent life exists elsewhere
> in the universe is that none of it has tried to contact us.     (Calvin)
>
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Raspunde prin e-mail lui