位置参数跟随关键字参数 [英] positional argument follows keyword argument

查看:45
本文介绍了位置参数跟随关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 python 中调用这样的函数.

I am a calling a function like this in python .

order_id = kite.order_place(self, exchange, tradingsymbol, 
transaction_type, quantity, price, product, order_type, validity, 
disclosed_quantity=None, trigger_price=None, squareoff_value, 
stoploss_value, trailing_stoploss, variety, tag='')

这是函数文档中的代码..

Here is the code from the documentation of the function ..

def order_place(self, exchange, tradingsymbol, transaction_type, 
quantity, price=None, product=None, order_type=None, validity=None, 
disclosed_quantity=None, trigger_price=None, squareoff_value=None, 
stoploss_value=None, trailing_stoploss=None, variety='regular', tag='')

它给出了这样的错误..

It is giving an error like this..

如何解决这个错误?谢谢!

How to resolve this error ? Thanks !

推荐答案

语法语言的指定位置参数出现在调用中的关键字或带星号的参数之前:

The grammar of the language specifies that positional arguments appear before keyword or starred arguments in calls:

argument_list        ::=  positional_arguments ["," starred_and_keywords]
                            ["," keywords_arguments]
                          | starred_and_keywords ["," keywords_arguments]
                          | keywords_arguments

具体来说,关键字参数如下所示:tag='insider trading!'而位置参数看起来像这样:..., exchange, ....问题在于您似乎复制/粘贴了参数列表,并保留了一些默认值,这使它们看起来像关键字参数而不是位置参数.这很好,只是您随后会返回使用位置参数,这是一个语法错误.

Specifically, a keyword argument looks like this: tag='insider trading!' while a positional argument looks like this: ..., exchange, .... The problem lies in that you appear to have copy/pasted the parameter list, and left some of the default values in place, which makes them look like keyword arguments rather than positional ones. This is fine, except that you then go back to using positional arguments, which is a syntax error.

此外,当参数具有默认值时,例如 price=None,这意味着您不必提供它.如果您不提供它,它将改用默认值.

Also, when an argument has a default value, such as price=None, that means you don't have to provide it. If you don't provide it, it will use the default value instead.

要解决此错误,请将后面的位置参数转换为关键字参数,或者,如果它们具有默认值并且您不需要使用它们,则根本不指定它们:

To resolve this error, convert your later positional arguments into keyword arguments, or, if they have default values and you don't need to use them, simply don't specify them at all:

order_id = kite.order_place(self, exchange, tradingsymbol,
    transaction_type, quantity)

# Fully positional:
order_id = kite.order_place(self, exchange, tradingsymbol, transaction_type, quantity, price, product, order_type, validity, disclosed_quantity, trigger_price, squareoff_value, stoploss_value, trailing_stoploss, variety, tag)

# Some positional, some keyword (all keywords at end):

order_id = kite.order_place(self, exchange, tradingsymbol,
    transaction_type, quantity, tag='insider trading!')

这篇关于位置参数跟随关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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