我想做:
award_dict = { "url" : "http://facebook.com", "imageurl" : "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count" : 1, } def award(name, count, points, desc_string, my_size, parent) : if my_size > count : a = { "name" : name, "description" : desc_string % count, "points" : points, "parent_award" : parent, } a.update(award_dict) return self.add_award(a, siteAlias, alias).award但是,如果在功能上感觉真的很麻烦,我会宁愿做的:
return self.add_award({ "name" : name, "description" : desc_string % count, "points" : points, "parent_award" : parent, }.update(award_dict), siteAlias, alias).award为什么不更新返回对象,以便可以链接?
JQuery做这个链接。 为什么python不能接受?
I 'm trying to do :
award_dict = { "url" : "http://facebook.com", "imageurl" : "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count" : 1, } def award(name, count, points, desc_string, my_size, parent) : if my_size > count : a = { "name" : name, "description" : desc_string % count, "points" : points, "parent_award" : parent, } a.update(award_dict) return self.add_award(a, siteAlias, alias).awardBut if felt really cumbersome in the function, and I would have rather done :
return self.add_award({ "name" : name, "description" : desc_string % count, "points" : points, "parent_award" : parent, }.update(award_dict), siteAlias, alias).awardWhy doesn't update return the object so you can chain?
JQuery does this to do chaining. Why isn't it acceptable in python?
最满意答案
Python主要实现了一个务实的命令查询分离的风格:mutator返回None (以实际的方式引发的例外,如pop ;-),所以它们不可能与访问器混淆(同样,分配不是表达式,语句表达式分离在那里,等等)。
这并不意味着,当你真正想要的时候,并没有很多方法来合并,例如, dict(a, **award_dict)使得一个新的命令就像你似乎希望的那样。为什么不使用THAT如果你真的觉得很重要?
编辑 :btw,没有必要,在你的具体情况下,创建a路途,或者:
dict(name=name, description=desc % count, points=points, parent_award=parent, **award_dict)创建一个与您的a.update(award_dict)具有完全相同语义的单个dict(包括在冲突的情况下), award_dict中的条目会覆盖您明确给出的事实;获取其他语义,即具有明确的条目“获胜”这样的冲突,通过作为唯一的位置 arg, 在关键字之前 ,并且没有**形式 - dict(award_dict, name=name等)的dict(award_dict, name=name 。
Python's mostly implementing a pragmatically tinged flavor of command-query separation: mutators return None (with pragmatically induced exceptions such as pop;-) so they can't possibly be confused with accessors (and in the same vein, assignment is not an expression, the statement-expression separation is there, and so forth).
That doesn't mean there aren't a lot of ways to merge things up when you really want, e.g., dict(a, **award_dict) makes a new dict much like the one you appear to wish .update returned -- so why not use THAT if you really feel it's important?
Edit: btw, no need, in your specific case, to create a along the way, either:
dict(name=name, description=desc % count, points=points, parent_award=parent, **award_dict)creates a single dict with exactly the same semantics as your a.update(award_dict) (including, in case of conflicts, the fact that entries in award_dict override those you're giving explicitly; to get the other semantics, i.e., to have explicit entries "winning" such conflicts, pass award_dict as the sole positional arg, before the keyword ones, and bereft of the ** form -- dict(award_dict, name=name etc etc).
更多推荐
发布评论