扩展dict类 [英] Extending the dict class

查看:67
本文介绍了扩展dict类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Pythoneers:


我需要将一个命名参数列表传递给给定的

订单中的函数,

并确保使用

中的keys()检索这些命名参数。例如:


keyargs = {}

keyargs [''one''] = 1

keyargs [''two' '] = 2

keyargs [''three''] = 3

myfunc(** keyargs)

-myfunc将使用keys()以相同的顺序检索关键参数

,因为它们已设置,即keyargs.keys()== [''one'',''two'',''three' ')


为实现这一目标,我将dict子类化并在__init __(),__ setitem __()和keys()中添加了所需的行

。然后我将dict分配给了我的新课程,但是只获得了字典所需的行为

创建如下:

d = dict()

但不适用于创建的字典:

d = {}



myfunc(** keyargs)


是否可以在这些情况下强制字典创建使用

我自己的dict类而不是默认类?


我想我们可以把它作为一个更通用的问题来表达:如果我想要修改字典类的行为,那么

是否有任何方法可以在解释器范围内进行那么特殊的dict构造函数

就像上面提到的那样使用修改后的版本?


感谢您的帮助

Hello Pythoneers:

I need to pass a list of named arguments to a function in a given
order,
and make sure these named arguments are retrieved using keys() in the
same order they were given. Example:

keyargs={}
keyargs[''one'']=1
keyargs[''two'']=2
keyargs[''three'']=3

myfunc(**keyargs)
-myfunc would retrieve key arguments with keys() in the same order
as they were set, i.e. keyargs.keys() == [''one'', ''two'', ''three'']

To achieve that, I subclassed dict and added the required lines
in __init__(), __setitem__() and keys(). I then assigned dict to
my new class but only get the desired behaviour for dictionaries
created like:
d=dict()
but not for dictionaries created like:
d={}
or
myfunc(**keyargs)

Is it possible to force dictionary creation in these case to use
my own dict class instead of the default one?

I guess we can formulate this as a more generic question: if I
want to modify the behaviour of the dictionary class, is there
any way to do it interpreter-wide so that special dict constructors
like those mentioned above use the modified version?

Thanks for helping

推荐答案

chosenchu写道:
chosechu wrote:

是否可以在这些情况下强制使用字典创建

我自己的dict类而不是默认类?
Is it possible to force dictionary creation in these case to use
my own dict class instead of the default one?




No


我想我们可以将此表示为一个更通用的问题:如果我

想要修改字典类的行为,有没有

任何方式来解释整个范围,以便特殊字典构造函数

如上所述使用修改后的版本?
I guess we can formulate this as a more generic question: if I
want to modify the behaviour of the dictionary class, is there
any way to do it interpreter-wide so that special dict constructors
like those mentioned above use the modified version?



并非没有修改和重新编译解释器。

Not without modifying and recompiling the interpreter.




Duncan Booth写道:

Duncan Booth wrote:

是否有可能强制字典创建在这种情况下使用

我自己的dict类代替默认的一个?
Is it possible to force dictionary creation in these case to use
my own dict class instead of the default one?





No



哎哟。我期待这样的事情,感谢你确认。


如果我可能:这似乎与我不一致。我创建了一个增强的

版本的类,没有可能扩展3个合法的

构造函数中的2个:{}和func(** d)。虽然我同意更改基类

行为是一个坏主意,因为我们有可能将

dict重新分配给另一个类,我们应该能够一致地执行它,即

也重新给予两位特殊建设者。


只是关于这个主题的2c ...无论如何我找到了解决办法。

Ouch. I was expecting something like that, thanks for confirming it.

If I may: this seems inconsistent to me. I have created an augmented
version of the class with no possibility to extend 2 of the 3 legal
constructors: {} and func(**d). While I agree changing base class
behaviour is a bad idea, since we have the possibility to re-assign
dict to another class we should be able to do it consistently, i.e.
re-assing the two special constructors too.

Just my 2c on the topic... I have found workarounds anyway.


chosenchu写道:
chosechu wrote:

Hello Pythoneers:


我需要将命名参数列表传递给给定的

命令中的函数,

并确保使用
$中的keys()检索这些命名参数b $ b给出了相同的订单。例如:


keyargs = {}

keyargs [''one''] = 1

keyargs [''two' '] = 2

keyargs [''three''] = 3

myfunc(** keyargs)

-myfunc将使用keys()以相同的顺序检索关键参数

,因为它们已设置,即keyargs.keys()== [''one'',''two'',''three' ']
Hello Pythoneers:

I need to pass a list of named arguments to a function in a given
order,
and make sure these named arguments are retrieved using keys() in the
same order they were given. Example:

keyargs={}
keyargs[''one'']=1
keyargs[''two'']=2
keyargs[''three'']=3

myfunc(**keyargs)
-myfunc would retrieve key arguments with keys() in the same order
as they were set, i.e. keyargs.keys() == [''one'', ''two'', ''three'']



我不确定你为什么要这样做 - 也许你可以

告诉你更多关于你的实际用途案例?


无论如何,由于它不是直接可能的,可能的解决方法

可以传递一系列(键,值)元组相反(可能是

* args)。这当然要求你可以修改实现

of myfunc()。

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p.split(''。'')])for br /'
p in''o **** @ xiludom.gro''。split(''@'')])"

I''m not sure to understand why you want to do so - perhaps you could
tell more about your real use case ?

Anyway, and since it''s not directly possible, a possible workaround
could be to pass a sequence of (key, value) tuples instead (possibly as
*args). This of course requires that you can modify the implementation
of myfunc().
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于扩展dict类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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