dict literals vs dict(** kwds) [英] dict literals vs dict(**kwds)

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

问题描述

虽然我认为dict(** kwds)是python中少数不幸的设计

选择之一,因为它阻止了将来添加有用的

关键字参数(例如一个默认值或一个orderby函数),我已经发现自己最近使用它而不是dict文字,

没有特别的原因。是否有任何编码风格的共识何时

应该dict文字比dict(** kwds)更受欢迎,反之亦然?


George

Although I consider dict(**kwds) as one of the few unfortunate design
choices in python since it prevents the future addition of useful
keyword arguments (e.g a default value or an orderby function), I''ve
been finding myself lately using it sometimes instead of dict literals,
for no particular reason. Is there any coding style consensus on when
should dict literals be preferred over dict(**kwds) and vice versa ?

George

推荐答案

George Sakkisaécrit:
George Sakkis a écrit :
虽然我认为dict(** kwds)是为数不多的不幸设计之一<在python中的选择,因为它阻止了将来添加有用的关键字参数(例如默认值或orderby函数),我已经发现自己最近使用它而不是dict文字,
没有特别的原因。是否有任何编码风格的共识,什么时候应该dict文字优于dict(** kwds),反之亦然?
Although I consider dict(**kwds) as one of the few unfortunate design
choices in python since it prevents the future addition of useful
keyword arguments (e.g a default value or an orderby function), I''ve
been finding myself lately using it sometimes instead of dict literals,
for no particular reason. Is there any coding style consensus on when
should dict literals be preferred over dict(**kwds) and vice versa ?




使用dict文字意味着你''我总是有一个内置字典 - 你

不能动态选择另一个类似dict的类。 OTHO,你只能使用有效的python标识符作为dict键(** kw)。



using dict literals means that you''ll always have a builtin dict - you
cannot dynamically select another dict-like class. OTHO, you can only
use valid python identifiers as keys with dict(**kw).


Bruno Desthuilliers写道:
Bruno Desthuilliers wrote:
George Sakkisaécrit:
George Sakkis a écrit :
虽然我认为dict(** kwds)是python中少数不幸的设计选择之一,因为它阻止了未来的添加有用的
关键字参数(例如默认值或orderby函数),我已经发现自己最近有时使用它而不是字典文字,
没有特别的原因。是否有任何编码风格的共识,什么时候应该dict文字优先于dict(** kwds),反之亦然?
Although I consider dict(**kwds) as one of the few unfortunate design
choices in python since it prevents the future addition of useful
keyword arguments (e.g a default value or an orderby function), I''ve
been finding myself lately using it sometimes instead of dict literals,
for no particular reason. Is there any coding style consensus on when
should dict literals be preferred over dict(**kwds) and vice versa ?



使用dict文字意味着你将永远有一个内置字典 - 你
不能动态选择另一个类似字典的类。 OTHO,你只能使用有效的python标识符作为dict键(** kw)。



using dict literals means that you''ll always have a builtin dict - you
cannot dynamically select another dict-like class. OTHO, you can only
use valid python identifiers as keys with dict(**kw).




这一切都很好但是没有回答我的问题原始问题:

情况(如果有的话)将{''name'':''Mike,''age'':23}首选

超过dict (name =''Mike'',年龄= 23),反之亦然,或者它只是一个问题

的味道,类似于使用单引号和双引号的字符串文字
(当两者都有效时)。


George



This is all good but doesn''t answer my original question: under which
circumstances (if any) would {''name'':''Mike, ''age'':23} be preferred
over dict(name=''Mike'', age=23) and vice versa, or if it''s just a matter
of taste, similar to using single vs double quote for string literals
(when both are valid of course).

George


George Sakkis< ge***********@gmail.com>写道:
George Sakkis <ge***********@gmail.com> wrote:
Bruno Desthuilliers写道:
Bruno Desthuilliers wrote:
George Sakkisaécrit:
George Sakkis a écrit :
虽然我考虑过dict(** kwds)作为python中为数不多的令人遗憾的设计选择之一,因为它阻止了将来添加有用的关键字参数(例如默认值或orderby函数),我已经被
最近发现自己有时使用它而不是字面文字,
没有特别的原因。是否有任何编码风格的共识,什么时候应该dict文字优先于dict(** kwds),反之亦然?
Although I consider dict(**kwds) as one of the few unfortunate design
choices in python since it prevents the future addition of useful
keyword arguments (e.g a default value or an orderby function), I''ve
been finding myself lately using it sometimes instead of dict literals,
for no particular reason. Is there any coding style consensus on when
should dict literals be preferred over dict(**kwds) and vice versa ?



使用dict文字意味着你将永远有一个内置字典 - 你
不能动态选择另一个类似字典的类。 OTHO,你只能使用有效的python标识符作为dict键(** kw)。



using dict literals means that you''ll always have a builtin dict - you
cannot dynamically select another dict-like class. OTHO, you can only
use valid python identifiers as keys with dict(**kw).



这一切都很好但是没有回答我原来的问题:哪个情况(如果有的话)将{''name'':''Mike','age'':23}优先于dict(name =''Mike'',年龄= 23反之亦然,或者它只是一个品味的问题,类似于使用单引号和双引号的字符串文字
(当两者都有效时)。



This is all good but doesn''t answer my original question: under which
circumstances (if any) would {''name'':''Mike, ''age'':23} be preferred
over dict(name=''Mike'', age=23) and vice versa, or if it''s just a matter
of taste, similar to using single vs double quote for string literals
(when both are valid of course).




我个人最喜欢的风格总是在适用时调用类型

(合理预测_remain_适用于未来的代码更改),

因为可读性有益。

Alex



My personal favorite style is always to call the type when applicable
(and reasonably forecast to _remain_ applicable in future code changes),
because readability benefits.
Alex


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

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