为什么没有python dict.update()返回对象?(Why doesn't a python dict.update() return the object?)

编程入门 行业动态 更新时间:2024-10-25 20:18:32
为什么没有python dict.update()返回对象?(Why doesn't a python dict.update() return the object?)

我想做:

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).award

But 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).award

Why 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).

更多推荐

本文发布于:2023-04-28 01:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   dict   python   update   object

发布评论

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

>www.elefans.com

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