作用于通过词典传递给方法的项目

编程入门 行业动态 更新时间:2024-10-22 10:45:24
本文介绍了作用于通过词典传递给方法的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的一个课程中定义了以下方法: def setup(self,items = {}): """ ;根据项目字典执行设置" 如果项目中有''某事'': value = items [''something''] #现在做一些有价值的东西 如果项目中有'另一个': value = items [''another''] #做其他有价值的东西 如果项目中有垃圾邮件: value = items [''spam''] #做另一件有价值的东西 如果项目中有''egg'': value = items [''eggs''] #等等 目的是根据传递给setup()的 字典中包含的值来设置对象方法。字典可能是空的,或 它可能包含可变数量的项目,并且它可能包含setup()方法查找的不是的项目。而且,这种方法可能在子类中被覆盖了。上面描述的方法工作正常,所以我想b $ b假设我应该单独留下足够好的东西,但我有这个唠叨 感觉Python有办法更简单地做到这一点或者更优雅。 有什么建议吗? Donnal Walter 阿肯色州儿童医院

The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if ''something'' in items: value = items[''something''] # now do something with value if ''another'' in items: value = items[''another''] # do something else with value if ''spam'' in items: value = items[''spam''] # do yet another thing with value if ''eggs'' in items: value = items[''eggs''] # and so on The purpose is to set up the object based on values contained in a dictionary passed to the setup() method. The dictionary may be empty, or it may contain a variable number of items, and it may contain items not looked for by the setup() method. Moreover, this method may be overridden in subclasses. The approach described above works ok, so I suppose I should leave well enough alone, but I have this nagging feeling that Python has a way to do this more simply or more elegantly. Any suggestion? Donnal Walter Arkansas Children''s Hospital

推荐答案

Donnal Walter写道: Donnal Walter wrote: 我的一个班级定义了以下方法: def setup (self,items = {}):"""执行基于项目字典的设置"" if if''something''in items: value = items [''something''] #现在做一些有价值的东西如果项目中有'另一个': value = items [''another''] #做有价值的事情如果项目中有垃圾邮件: value = it ems [''spam''] #还有另一件有价值的东西如果项目中有''egg'': value = items [''eggs''] #等等 任何建议? The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if ''something'' in items: value = items[''something''] # now do something with value if ''another'' in items: value = items[''another''] # do something else with value if ''spam'' in items: value = items[''spam''] # do yet another thing with value if ''eggs'' in items: value = items[''eggs''] # and so on Any suggestion?

首先,不要使用{}作为项目的默认值。许多人已经把这个陷阱变成了b $ b:虽然完全合法,但它只会被评估一次,当第一次找到方法设置时,它会被评估。那么一个dict的实例是 创建的。但是现在如果随后的设置调用改变了dict项目指向 ,它们都会共享相同的字典!这个小例子说明了 的行为: def foo(key,bar = {}): bar [key] = key 打印酒吧 $ x $ b for x in xrange(3): foo(i) 收益率: {1:1} {1:1,2:2} 你肯定在第二种情况下预期{2:2}。所以 的成语通常是这样的: def foo(key,bar = None): 如果bar是无: bar = {}#remember:bar是一个本地名称,绑定到一个值 - 所以接下来 时间,如果调用foo,bar仍然是none / o栏作为参数 bar [key] = key 现在问题:这个有用: 对于[''''',''另一个'',''垃圾'',''鸡蛋''中的k: if items.has_key(k): value = items [k] break - 问候, Diez B. Roggisch

First of all, don''t use {} as default value for items. Many have stepped into this trap: While perfectly legal, it will be evaluated only once, when the method setup is found the first time. So then an instance of a dict is created. But now if subsequent calls to setup alter that dict items points to, they all share the same dict!! This small example illustrates that behaviour: def foo(key, bar={}): bar[key] = key print bar for i in xrange(3): foo(i) yields: {1:1} {1:1, 2:2} where you surely have expected {2:2} in the second case. So the idiom for this commonly is this: def foo(key, bar=None): if bar is None: bar = {} # remember: bar is a local name, bound to a value - so next time, bar will still be none if foo is called w/o bar as parameter bar[key] = key Now to your question: This works: for k in [''something'', ''another'', ''spam'', ''eggs'']: if items.has_key(k): value = items[k] break -- Regards, Diez B. Roggisch

>>>>> " Donnal" == Donnal Walter< do **** @ donnal>写道: Donnal>如果项目中有''某事': Donnal> value = items [''something''] Donnal> #现在做一些有价值的东西 Donnal>如果项目中有另一个: Donnal> value = items [''another''] Donnal> #做有价值的事情 Donnal>在子类中重写。上述方法 Donnal>工作正常,所以我想我应该单独留下, Donnal>但我有这种唠叨的感觉,Python有办法 Donnal>更简单或更优雅地做到这一点。有什么建议吗? 代码很好 - 你可以通过使用 来避免重复''某事' a.get(k [,x]) #a [k]如果k在a中,否则x 如果你觉得''聪明''并且不关心表现(不,我没有&b 基准): - --- def dictget(d,key): 如果键入d: 收益率d [key] 返回 d = {''垃圾邮件'':1,''鸡蛋'':67} 表示dictget中的值(d,''垃圾邮件''): 打印价值 表示dictget值(d,''larch''): 打印值+ 999#从不打印 表示dictget值(d,''eggs''): 打印值+ 2 ----- 好​​吧,即使我这样说,这几乎也是食谱材料...... ;-) - Ville Vainio tinyurl/2prnb >>>>> "Donnal" == Donnal Walter <do****@donnal> writes: Donnal> if ''something'' in items: Donnal> value = items[''something''] Donnal> # now do something with value Donnal> if ''another'' in items: Donnal> value = items[''another''] Donnal> # do something else with value Donnal> overridden in subclasses. The approach described above Donnal> works ok, so I suppose I should leave well enough alone, Donnal> but I have this nagging feeling that Python has a way to Donnal> do this more simply or more elegantly. Any suggestion? The code is quite ok - you might avoid the repetition of ''something'' by using a.get(k[, x]) # a[k] if k in a, else x If you feel ''clever'' and don''t care about performance (no, I didn''t benchmark this): ----- def dictget(d, key): if key in d: yield d[key] return d = { ''spam'' : 1, ''eggs'' : 67 } for value in dictget(d,''spam''): print value for value in dictget(d,''larch''): print value + 999 # never printed for value in dictget(d,''eggs''): print value + 2 ----- Well well, that''s almost cookbook material even if I say so myself... ;-) -- Ville Vainio tinyurl/2prnb

2004年10月15日星期五11:56: 12月05日,Donnal Walter写道: On Fri, 15 Oct 2004 11:56:12 -0500, Donnal Walter wrote: 我的一个班级定义了以下方法: def setup(self,items = {}): """执行基于项目词典的设置"" 如果项目中有''某事'': value = items [''something''] #现在做一些有价值的事情 The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if ''something'' in items: value = items[''something''] # now do something with value

.... 这主要取决于做某事的内容价值 你到处都是胡椒。我会把各种各样的做一些事情 拉出来制作方法,但除此之外,如果你需要一个可以包含任何内容的 dict的完全灵活性而且你需要用任意的 组合来做事,没什么可做的。 把事情搞砸到方法后,我实际上会转移 ;如果 语句进入方法并执行以下操作: def setup(self,items = None): 如果项目为无: items = {} self.items = items for method in(''something'',''another'',''spam'',''eggs''): getattr(self,method)() 这将使其在长期内更加灵活。 其他一些事情可以想到(可能想要分开self.item $ b循环中的$ b设置,因此孩子们可以覆盖循环而不会弄乱c中的项目超级班的所有人;如果你不知道我的意思 因为我在这里有点模糊,不要担心:-)),但这是关于 所有你可以做而不会变得愚蠢。

.... It mostly depends on the contents of the "do something with value" that you have peppered throughout. I would pull out the various "do somethings" into methods, but other then that, if you need the full flexibility of a dict that can contain anything and you need to do things with arbitrary combinations, there is little else to do. After pulling things out into methods, I''d actually shift the "if" statement into the method and do something like the following: def setup(self, items = None): if items is None: items = {} self.items = items for method in (''something'', ''another'', ''spam'', ''eggs''): getattr(self, method)() which will make it more flexible in the long term. A few other things leap to mind (might want to separate the self.item setting from the loop, so children can override the loop without messing up items in calls to the super class; if you don''t know what I mean because I''m being somewhat vague here, don''t worry :-) ), but that''s about all you can do without getting silly.

更多推荐

作用于通过词典传递给方法的项目

本文发布于:2023-10-25 13:48:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作用于   词典   方法   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!