最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can you have required keyword arguments in Javascript or Python? - Stack Overflow

programmeradmin5浏览0评论

Can you have required keyword arguments in javascript or python? Is this a common feature of programming languages, or is it new and rare? They would be analogous to this implementation of keyword arguments in Ruby in Ruby 2.1+

def obvious_total(subtotal:, tax:, discount:)
  subtotal + tax - discount
end

obvious_total(subtotal: 100, tax: 10, discount: 5) # => 105

(The above example comes directly from )

I'd like to know because I was really interested in the perspective of the author of the above page. He basically suggests that required keyword arguments would help coders understand each other's code later on down the line, while only sacrificing succintness. Personally, I think that that is a decent trade off, and I wonder if it is commonly practiced or not.

It is quite a common occurrence, I think, to find poorly documented code, and to wonder which argument does what. That's why I always try to put good and succinct instructions at my methods. I could be crazy and this is a completely unnecessary feature, after all, I'm just a newbie coder who scripts stuff when he gets lazy.

Can you have required keyword arguments in javascript or python? Is this a common feature of programming languages, or is it new and rare? They would be analogous to this implementation of keyword arguments in Ruby in Ruby 2.1+

def obvious_total(subtotal:, tax:, discount:)
  subtotal + tax - discount
end

obvious_total(subtotal: 100, tax: 10, discount: 5) # => 105

(The above example comes directly from https://robots.thoughtbot.com/ruby-2-keyword-arguments)

I'd like to know because I was really interested in the perspective of the author of the above page. He basically suggests that required keyword arguments would help coders understand each other's code later on down the line, while only sacrificing succintness. Personally, I think that that is a decent trade off, and I wonder if it is commonly practiced or not.

It is quite a common occurrence, I think, to find poorly documented code, and to wonder which argument does what. That's why I always try to put good and succinct instructions at my methods. I could be crazy and this is a completely unnecessary feature, after all, I'm just a newbie coder who scripts stuff when he gets lazy.

Share Improve this question asked Jun 15, 2016 at 7:55 Thonanth SiddefThonanth Siddef 1632 silver badges8 bronze badges 7
  • 1 For Python, see: python.org/dev/peps/pep-3102 – jonrsharpe Commented Jun 15, 2016 at 8:00
  • I'd argue that this is a case of being verbose simply for the sake of being verbose. It is not at all difficult to figure out function parameters, and if a developer needs to change/analyze the function he has to look it up anyway. EDIT: After reading the Python rationale, I can see how keyword-only parameters can be useful. Still, if you need a keyword parameter, you may be better of refactoring, IMO. – filpa Commented Jun 15, 2016 at 8:03
  • Your title asks "can you", which is OK, but your question implies you want an answer to "should you", which is too opinion-based for SO. – jonrsharpe Commented Jun 15, 2016 at 8:14
  • @jonrsharpe I guess I did get kind of off topic in my reasoning behind wanting to ask the question, but that doesn't change the fact of the question. Is it a common feature among different programming languages? More specifically, Python/Javascript? I see now that there is, in fact, an implementation in Python, but I still have no idea what the situation is for Javascript. I could edit the question or question title if you think that that would help the question fit the SO format better. – Thonanth Siddef Commented Jun 16, 2016 at 7:42
  • @user991710 Well, I would think the point of the required keyword parameter is to use it when you don't need to. Like having to specify an id in a function would be as easy as func(id: 1), and it makes it clear that you are to supply an id of some sort, rather than a string or other object. – Thonanth Siddef Commented Jun 16, 2016 at 7:44
 |  Show 2 more comments

1 Answer 1

Reset to default 25

PEP-3102 introduced "keyword-only arguments", so in Python 3.x you could do:

def obvious_total(*, subtotal, tax, discount):
    """Calculate the total and force keyword arguments."""
    return subtotal + tax - discount

In use:

>>> obvious_total(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: obvious_total() takes 0 positional arguments but 3 were given
>>> obvious_total(subtotal=1, tax=2, discount=3)
0

In JavaScript there is no way to have keyword arguments at all. The convention for this would be to define a single positional argument into which the user passes an object literal:

function obviousTotal(input) {
    return input.subtotal + input.tax - input.discount
}

and then pass an object literal as input. In use:

> obviousTotal({ subtotal: 1, tax: 2, discount: 3 })
0

ES6 lets you simplify this a bit with destructuring:

function obviousTotal({ discount, subtotal, tax }) {
    return subtotal + tax - discount;
}

but there is still no native support for required keyword arguments.

发布评论

评论列表(0)

  1. 暂无评论