任何理由不总是使用关键字参数? [英] Any reason NOT to always use keyword arguments?

查看:95
本文介绍了任何理由不总是使用关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在跳进python之前,我开始学习一些Objective-C / Cocoa的书。我记得,大多数函数需要明确说明关键字参数。直到最近我忘了所有这一点,只是在Python中使用位置参数。但最近,我遇到了一些错误,导致不当的立场 - 鬼祟的小事情,他们是。



让我想到 - 一般来说,除非有特定的情况需要非关键字参数 - 是否有任何好的理由不使用关键字参数?它被认为是坏的风格,总是使用它们,即使是简单的功能?



我觉得我的大多数50行程序已定期扩展到500行或更多行,如果我习惯于总是使用关键字参数,代码将随着它的增长更容易可读和可维护。任何原因可能不是这样?



UPDATE:



我得到的总体印象是它的风格偏好,有许多好的论据,他们通常不应该用于非常简单的论点,但在其他方面符合良好的风格。在接受之前,我只想澄清一下 - 是否有任何特定的非风格问题,从这种方法 - 例如,重大的性能命中?

解决方案

除了代码的清晰性和可读性之外,没有任何理由不使用关键字参数。



我遵循以下一般规则:


  1. 如果很难从函数名称推断参数的函数(名称) - 通过关键字传递参数(例如,我不想在我的代码中有 text.splitlines(True)

  2. 如果很难推断参数的顺序,例如如果你有太多的参数,或者你有独立的可选参数 - 通过关键字传递(例如 funkyplot(x,y,None,None,None,None,None,None,'red' / code>看起来不是特别好)。

  3. 如果参数的用途明显,不要通过关键字传递前几个参数。你会看到, sin(2 * pi) sin(value = 2 * pi) c> ,稳定的强制性参数将是位置的,可选的参数将是关键字。



    性能也可能有差异,因为在每个实现中,但是考虑到这通常是一个过早的优化,并且其结果不会显着,我不认为这是决定的关键。



    更新:非分类问题



    关键字参数可以处理位置参数可以做的一切,如果您要定义新的API,可能的性能问题。



    请考虑以下方面:




    • 如果你使你的函数接受关键字参数,那就成为你的接口的一部分。
      你不能用另一个具有类似签名但是对同一个参数使用不同关键字的函数来替换你的函数。

    • 你可能想使用装饰器或另一个实用程序函数,假设您的函数接受位置参数。未绑定的方法是这样的实用程序的一个例子,因为它们总是在读取位置后传递第一个参数作为位置,所以 cls.method(self = cls_instance)如果在定义中有一个参数 self



    如果你设计你的API和文档使用关键字参数,尤其是如果你不是设计的东西应该与已经存在的东西是可互换的。如果你是一个真正的问题。


    Before jumping into python, I had started with some Objective-C / Cocoa books. As I recall, most functions required keyword arguments to be explicitly stated. Until recently I forgot all about this, and just used positional arguments in Python. But lately, I've ran into a few bugs which resulted from improper positions - sneaky little things they were.

    Got me thinking - generally speaking, unless there is a circumstance that specifically requires non-keyword arguments - is there any good reason NOT to use keyword arguments? Is it considered bad style to always use them, even for simple functions?

    I feel like as most of my 50-line programs have been scaling to 500 or more lines regularly, if I just get accustomed to always using keyword arguments, the code will be more easily readable and maintainable as it grows. Any reason this might not be so?

    UPDATE:

    The general impression I am getting is that its a style preference, with many good arguments that they should generally not be used for very simple arguments, but are otherwise consistent with good style. Before accepting I just want to clarify though - is there any specific non-style problems that arise from this method - for instance, significant performance hits?

    解决方案

    There isn't any reason not to use keyword arguments apart from the clarity and readability of the code. The choice of whether to use keywords should be based on whether the keyword adds additional useful information when reading the code or not.

    I follow the following general rule:

    1. If it is hard to infer the function (name) of the argument from the function name – pass it by keyword (e.g. I wouldn't want to have text.splitlines(True) in my code).
    2. If it is hard to infer the order of the arguments, for example if you have too many arguments, or when you have independent optional arguments – pass it by keyword (e.g. funkyplot(x, y, None, None, None, None, None, None, 'red') doesn't look particularly nice).
    3. Never pass the first few arguments by keyword if the purpose of the argument is obvious. You see, sin(2*pi) is better than sin(value=2*pi), the same is true for plot(x, y, z).

    In most cases, stable mandatory arguments would be positional, and optional arguments would be keyword.

    There's also a possible difference in performance, because in every implementation the keyword arguments would be slightly slower, but considering this would be generally a premature optimisation and the results from it wouldn't be significant, I don't think it's crucial for the decision.

    UPDATE: Non-stylistical concerns

    Keyword arguments can do everything that positional arguments can, and if you're defining a new API there are no technical disadvantages apart from possible performance issues. However, you might have little issues if you're combining your code with existing elements.

    Consider the following:

    • If you make your function take keyword arguments, that becomes part of your interface. You can't replace your function with another that has a similar signature but a different keyword for the same argument.
    • You might want to use a decorator or another utility on your function that assumes that your function takes a positional argument. Unbound methods are an example of such utility because they always pass the first argument as positional after reading it as positional, so cls.method(self=cls_instance) doesn't work even if there is an argument self in the definition.

    None of these would be a real issue if you design your API well and document the use of keyword arguments, especially if you're not designing something that should be interchangeable with something that already exists.

    这篇关于任何理由不总是使用关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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