是否有短路词典“get”方法? [英] Is there a short-circuiting dictionary "get" method?

查看:60
本文介绍了是否有短路词典“get”方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个片段中:


d = {''x'':1}

value = d.get(''x'',bigscaryfunction ())


总是调用bigscary函数,即使''x''是有效密钥。

是否存在短路信号。如果第一个参数是有效密钥,则不会评估

第二个参数的get版本吗?现在我会编码

它,但这种行为让我感到有些惊讶......


Dave

解决方案

Dave Opstad写道:

在这个片段中:

d = {''x'':1}
value = d.get(''x'',bigscaryfunction())

总是调用bigscary函数,即使''x''是有效密钥。
是否有"短路"如果第一个是有效密钥,那么不能评估
第二个参数的get版本吗?现在我会给它编码
它,但这种行为让我感到有些惊讶......




试试:

value = d [''x'']

除了KeyError:

value = bigscaryfunction()


get()是只是一个方法,方法的参数总是在传递给方法之前进行评估,所以简短的

答案是不,没有''版本' 'get()会做什么

你想要什么"


-Peter


戴夫,


2005年3月9日星期三09:45:41 -0800,Dave Opstad< op **** @ batnet.com>写道:

在这个片段中:

d = {''x'':1}
value = d.get(''x'',bigscaryfunction ())

总是调用bigscary函数,即使x是有效密钥。
是否存在短路信号。如果第一个是有效密钥,那么不能评估
第二个参数的get版本吗?现在我会编码
它,但这种行为让我感到有些惊讶......




没有像你这样的短路功能'要求,因为

它在python中是不可能的。要将参数传递给''get''函数,

python在调用'get'之前评估bigscaryfunction。


(我相信这意味着那个python没有懒惰的评价,但

语言律师可能会把我打倒。维基百科似乎说

这意味着python没有'没有'延迟评估')。


这里有两种方法可以做你想做的事:


if''x''在d:value = d [''x'']

else:value = bigscaryfunction()


或:


def sget(dict,key,func,* args):

如果键入dict:返回键

else:return func(* args)


sget(d,''x'',bigscaryfunction)


这两种方法都是未经测试的,但应该稍作修改。


和平

Bill Mill

bill.mill at gmail.com


也许这可以帮助:


value = d.get(''x'',lambda:bigscaryfunction())

Bearophile


In this snippet:

d = {''x'': 1}
value = d.get(''x'', bigscaryfunction())

the bigscaryfunction is always called, even though ''x'' is a valid key.
Is there a "short-circuit" version of get that doesn''t evaluate the
second argument if the first is a valid key? For now I''ll code around
it, but this behavior surprised me a bit...

Dave

解决方案

Dave Opstad wrote:

In this snippet:

d = {''x'': 1}
value = d.get(''x'', bigscaryfunction())

the bigscaryfunction is always called, even though ''x'' is a valid key.
Is there a "short-circuit" version of get that doesn''t evaluate the
second argument if the first is a valid key? For now I''ll code around
it, but this behavior surprised me a bit...



try:
value = d[''x'']
except KeyError:
value = bigscaryfunction()

get() is just a method, and arguments to methods are always
evaluated before being passed to the method, so the short
answer is "no, there is no ''version'' of get() that will do
what you want".

-Peter


Dave,

On Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad <op****@batnet.com> wrote:

In this snippet:

d = {''x'': 1}
value = d.get(''x'', bigscaryfunction())

the bigscaryfunction is always called, even though ''x'' is a valid key.
Is there a "short-circuit" version of get that doesn''t evaluate the
second argument if the first is a valid key? For now I''ll code around
it, but this behavior surprised me a bit...



There is no short-circuit function like you''re asking for, because
it''s impossible in python. To pass an argument to the ''get'' function,
python evaluates the bigscaryfunction before calling ''get''.

(I believe this means that python doesn''t have "lazy evaluation", but
the language lawyers may shoot me down on that. Wikipedia seems to say
that it means python doesn''t have "delayed evaluation").

Here are two ways to do what you want:

if ''x'' in d: value = d[''x'']
else: value = bigscaryfunction()

or:

def sget(dict, key, func, *args):
if key in dict: return key
else: return func(*args)

sget(d, ''x'', bigscaryfunction)

Both methods are untested, but should work with minor modifications.

Peace
Bill Mill
bill.mill at gmail.com


Maybe this can help:

value = d.get(''x'', lambda: bigscaryfunction())

Bearophile


这篇关于是否有短路词典“get”方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆