kwargs 解析最佳实​​践

发布于 2024-10-31 08:54:01 字数 323 浏览 2 评论 0原文

有没有更紧凑/有效的方法来做到这一点?

    for key in kwargs:
        if key == 'log':
            self.log = kwargs[key]
        elif key == 'bin':
            self.bin = kwargs[key]
        elif key == 'pid':
            self.pid = kwargs[key]
        elif key == 'conf':
            self.conf = kwargs[key]

Is there a more compact/efficient way of doing this?

    for key in kwargs:
        if key == 'log':
            self.log = kwargs[key]
        elif key == 'bin':
            self.bin = kwargs[key]
        elif key == 'pid':
            self.pid = kwargs[key]
        elif key == 'conf':
            self.conf = kwargs[key]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

橘味果▽酱 2024-11-07 08:54:01

要准确实现您的要求,您可以使用

for key in ('log', 'bin', 'pid', 'conf'):
    if key in kwargs:
        setattr(self, key, kwargs[key])

or

self.__dict__.update((key, kwargs[key])
                     for key in ('log', 'bin', 'pid', 'conf')
                     if key in kwargs)

但是,我通常更喜欢这样的东西:

def f(log=None, bin=None, pid=None, conf=None):
    self.log = log
    self.bin = bin
    self.pid = pid
    self.conf = conf

虽然这仍然有些重复,但代码确实很容易阅读。无论是否传入相应的关键字参数,所有属性都会被初始化,并且函数的签名清楚地记录了参数和默认值。

To achieve exactly what you asked for, you could use

for key in ('log', 'bin', 'pid', 'conf'):
    if key in kwargs:
        setattr(self, key, kwargs[key])

or

self.__dict__.update((key, kwargs[key])
                     for key in ('log', 'bin', 'pid', 'conf')
                     if key in kwargs)

However, I would generally prefer something like this:

def f(log=None, bin=None, pid=None, conf=None):
    self.log = log
    self.bin = bin
    self.pid = pid
    self.conf = conf

While this is still somewhat repetitive, the code is really easy to read. All attributes are intialized regardles of whether the corresponding keyword argument is passed in, and the signature of the function clearly documents the arguments and there defaults.

断桥再见 2024-11-07 08:54:01
self.log = kwargs.get('log', default_log)
self.bin = kwargs.get('bin', default_bin)
self.pid = kwargs.get('pid', default_pid)
self.conf = kwargs.get('conf', default_conf)

这还有一个额外的优点,即在任何情况下都会分配 self.logAttributeError 意味着您的代码已经严重损坏,仅此而已。始终确保所有内容都已分配。) 。没有额外的 self.log = default_log 行。您可以省略默认值以获得None

self.log = kwargs.get('log', default_log)
self.bin = kwargs.get('bin', default_bin)
self.pid = kwargs.get('pid', default_pid)
self.conf = kwargs.get('conf', default_conf)

This has the additional advantage that self.log is assigned in any case (AttributeError means your code is broken as hell, nothing more. Always make sure everything is always assigned.). Without extra self.log = default_log lines. You can omit the default to get None.

樱花坊 2024-11-07 08:54:01

如果 get() 中提供的键不在字典中,则结果为 None

self.log = kwargs.get('log')
self.bin = kwargs.get('bin')
self.pid = kwargs.get('pid')
self.conf = kwargs.get('conf')

If the key provided in get() is not in the dictionary the result is None.

self.log = kwargs.get('log')
self.bin = kwargs.get('bin')
self.pid = kwargs.get('pid')
self.conf = kwargs.get('conf')
抱猫软卧 2024-11-07 08:54:01
for k,v in kwarg.iteritems():
   setattr(self, k, v)

其中 setattr(self, "bin", "val") 就像调用 self.bin = "val"

但是更希望有一个像 @Sven 这样的白名单马尔纳克有。

for k,v in kwarg.iteritems():
   setattr(self, k, v)

In which setattr(self, "bin", "val") is like calling self.bin = "val"

However it is more desirable to have a whitelist like @Sven Marnach has.

原来分手还会想你 2024-11-07 08:54:01
for k,v in kw.items():
   setattr(self, k, v)
for k,v in kw.items():
   setattr(self, k, v)
再见回来 2024-11-07 08:54:01

self.__dict__.update(kwargs)

self.__dict__.update(kwargs)

濫情▎り 2024-11-07 08:54:01

我的解决方案是:

for key in ('log', 'bin', 'pid', 'conf'):
    setattr(self, key, kwargs.get(key, None))

在这种模式下,所有属性都被初始化。

当我有大量属性时,我更喜欢创建一个列表以便更容易阅读,如下所示:

kwargs_list = [
    "log",
    "bin",
    "pin",
    "conf"
]

for key in kwargs_list:
    setattr(self, key, kwargs.get(key, None))

My solution for this is:

for key in ('log', 'bin', 'pid', 'conf'):
    setattr(self, key, kwargs.get(key, None))

In this mode, all attributes are initialized.

When I have a large number of attributes, I prefer to create a list to be easier to read like this:

kwargs_list = [
    "log",
    "bin",
    "pin",
    "conf"
]

for key in kwargs_list:
    setattr(self, key, kwargs.get(key, None))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文