Pre-PEP:字典累加器方法 [英] Pre-PEP: Dictionary accumulator methods

查看:79
本文介绍了Pre-PEP:字典累加器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让大家对两种新的字典方法有所了解:


def count(self,value,qty = 1):

尝试:

self [key] + = qty

除了KeyError:

self [key] = qty


def appendlist(自我,关键,*值):

尝试:

self [key] .extend(values)

除了KeyError:

self [key] = list(values)


理由是替换字典的尴尬和缓慢的现有习语
基于
的积累:


d [key] = d.get(key,0)+ qty

d.setdefault(key,[]) .extend(values)


在最简单的形式中,这两个语句现在可以更加可读地编码为:


d.count(key)

d.appendlist(键,值)


在多值表格中,它们现在编码为:


d.count(key,qty)

d.appendlist(键,*值)


新方法返回的错误消息是Ť与现有成语返回的那些相同。


get()方法将继续存在,因为它对应用程序很有用

除了累积。


setdefault()方法将继续存在,但可能不会使它成为Py3.0中的


问题已解决

---------------------


现有结构的可读性问题是:


*他们教授,创建,阅读和评论都很尴尬。

*他们的措辞倾向于隐藏真实意义(积累)。

* setdefault()方法名称的含义不言自明。


现有结构的性能问题是:


*它们会转化为许多操作码,这会大大减慢它们的速度。

* get()习惯用法需要对同一个键进行两次字典查找。 />
* setdefault()idiom在每个之前实例化一个新的空列表打电话。

*通常不需要新列表并立即丢弃。

* setdefault()惯用法需要对extend / append进行属性查找。

* setdefault()成语进行两次函数调用。


后面的问题从反汇编中可见:

I would like to get everyone''s thoughts on two new dictionary methods:

def count(self, value, qty=1):
try:
self[key] += qty
except KeyError:
self[key] = qty

def appendlist(self, key, *values):
try:
self[key].extend(values)
except KeyError:
self[key] = list(values)

The rationale is to replace the awkward and slow existing idioms for dictionary
based accumulation:

d[key] = d.get(key, 0) + qty
d.setdefault(key, []).extend(values)

In simplest form, those two statements would now be coded more readably as:

d.count(key)
d.appendlist(key, value)

In their multi-value forms, they would now be coded as:

d.count(key, qty)
d.appendlist(key, *values)

The error messages returned by the new methods are the same as those returned by
the existing idioms.

The get() method would continue to exist because it is useful for applications
other than accumulation.

The setdefault() method would continue to exist but would likely not make it
into Py3.0.
PROBLEMS BEING SOLVED
---------------------

The readability issues with the existing constructs are:

* They are awkward to teach, create, read, and review.
* Their wording tends to hide the real meaning (accumulation).
* The meaning of setdefault() ''s method name is not self-evident.

The performance issues with the existing constructs are:

* They translate into many opcodes which slows them considerably.
* The get() idiom requires two dictionary lookups of the same key.
* The setdefault() idiom instantiates a new, empty list prior to every call.
* That new list is often not needed and is immediately discarded.
* The setdefault() idiom requires an attribute lookup for extend/append.
* The setdefault() idiom makes two function calls.

The latter issues are evident from a disassembly:

dis(compile(''d [key] = d.get(key,0)+ qty'',''''''''exec''))
1 0 LOAD_NAME 0(d)

3 LOAD_ATTR 1(获取)

6 LOAD_NAME 2(关键)

9 LOAD_CONST 0 (0)

12 CALL_FUNCTION 2

15 LOAD_NAME 3(数量)

18 BINARY_ADD

19 LOAD_NAME 0( d)

22 LOAD_NAME 2(关键)

25 STORE_SUBSCR

26 LOAD_CONST 1(无)

29 RETURN_VALUE

dis(com pile(''d.setdefault(key,[])。extend(values)'',''''''''exec''))
dis(compile(''d[key] = d.get(key, 0) + qty'', '''', ''exec'')) 1 0 LOAD_NAME 0 (d)
3 LOAD_ATTR 1 (get)
6 LOAD_NAME 2 (key)
9 LOAD_CONST 0 (0)
12 CALL_FUNCTION 2
15 LOAD_NAME 3 (qty)
18 BINARY_ADD
19 LOAD_NAME 0 (d)
22 LOAD_NAME 2 (key)
25 STORE_SUBSCR
26 LOAD_CONST 1 (None)
29 RETURN_VALUE
dis(compile(''d.setdefault(key, []).extend(values)'', '''', ''exec''))



1 0 LOAD_NAME 0(d)

3 LOAD_ATTR 1(setdefault)

6 LOAD_NAME 2(关键)

9 BUILD_LIST 0

12 CALL_FUNCTION 2

15 LOAD_ATTR 3(延期)

18 LOAD_NAME 4(价值)

21 CALL_FUNCTION 1

24 POP_TOP

25 LOAD_CONST 0(无)

28 RETURN_VALUE


相比之下,建议方法只使用单个属性查找和

函数调用,它们只使用一个字典查找,它们使用很少的操作码,

并且它们直接访问累积函数PyNumber_Add( )或

PyList_Append()。 IOW,性能提升与可读性相匹配

改进。

问题

------


建议的名称可能会有所改进(也许tally()更活跃

并且清除比count())。


附加列表()方法不像setdefault()那样通用,可以与其他对象类型一起使用

(也许用于创建字典词典)。

然而,大多数用途我已经看到的是列表。对于其他用途,纯Python代码

就速度,清晰度和避免不必要的实例化而言足够了

空容器:


如果密钥不在d中:

d.key = {subkey:value}

else:

d [key] [subkey] = value


Raymond Hettinger


1 0 LOAD_NAME 0 (d)
3 LOAD_ATTR 1 (setdefault)
6 LOAD_NAME 2 (key)
9 BUILD_LIST 0
12 CALL_FUNCTION 2
15 LOAD_ATTR 3 (extend)
18 LOAD_NAME 4 (values)
21 CALL_FUNCTION 1
24 POP_TOP
25 LOAD_CONST 0 (None)
28 RETURN_VALUE

In contrast, the proposed methods use only a single attribute lookup and
function call, they use only one dictionary lookup, they use very few opcodes,
and they directly access the accumulation functions, PyNumber_Add() or
PyList_Append(). IOW, the performance improvement matches the readability
improvement.
ISSUES
------

The proposed names could possibly be improved (perhaps tally() is more active
and clear than count()).

The appendlist() method is not as versatile as setdefault() which can be used
with other object types (perhaps for creating dictionaries of dictionaries).
However, most uses I''ve seen are with lists. For other uses, plain Python code
suffices in terms of speed, clarity, and avoiding unnecessary instantiation of
empty containers:

if key not in d:
d.key = {subkey:value}
else:
d[key][subkey] = value

Raymond Hettinger

推荐答案

大家好 -

也许我'我没有得到它,但我认为更好的名字将是

add。如在


d.add(键)

d.add(键,-1)

d.add(键) ,399)




Raymond Hettinger写道:
Hi All--
Maybe I''m not getting it, but I''d think a better name for count would be
add. As in

d.add(key)
d.add(key,-1)
d.add(key,399)
etc.

Raymond Hettinger wrote:

我想得到每个人的's关于两种新词典方法的想法:

def count(self,value,qty = 1):
尝试:
self [key] + = qty
除了KeyError :
self [key] = qty

I would like to get everyone''s thoughts on two new dictionary methods:

def count(self, value, qty=1):
try:
self[key] += qty
except KeyError:
self[key] = qty




字典没有现有的add()方法。鉴于名称

的变化,我希望看到它。


Metta,

伊凡

---------------------------------------------- < br $>
Ivan Van Laningham

God N机车工程
http://www.pauahtun.org/
http://www.andi-holmes.com/

陆军通信兵团:Cu Chi,'70岁'

作者:自学Python 24小时



There is no existing add() method for dictionaries. Given the name
change, I''d like to see it.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.pauahtun.org/
http://www.andi-holmes.com/
Army Signal Corps: Cu C Class of ''70
Author: Teach Yourself Python in 24 Hours


Raymond Hettinger写道:
Raymond Hettinger wrote:
def count(self,value,qty = 1):
尝试:
self [key] + = qty
除了KeyError:
self [key] = qty
def count(self, value, qty=1):
try:
self[key] += qty
except KeyError:
self[key] = qty




我假设参数列表是一个错字,实际上应该是


def count(self,key,qty = 1):...


正确吗?


Jeff Shannon



I presume that the argument list is a typo, and should actually be

def count(self, key, qty=1): ...

Correct?

Jeff Shannon


文章< JbL_d.8237
In article <JbL_d.8237


这篇关于Pre-PEP:字典累加器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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