python - 位置参数跟随关键字参数

标签 python

<分区>

我正在 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='')

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

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='')

它给出了这样的错误..

enter image description here

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

最佳答案

grammar of the language指定位置参数出现在调用中的关键字或星号参数之前:

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

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

另外,当一个参数有一个默认值时,例如price=None,这意味着您不必提供它。如果您不提供它,它将使用默认值代替。

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

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!')

关于python - 位置参数跟随关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42163846/

相关文章:

python - nodeenv -p 在 vi​​rtualenv 中不起作用

Python Panda 数据框 - 从字符串到日期的整行

python - 如何从 pandas 图中提取数据?

python - 找不到 Pandas Series.dt.total_seconds()

javascript - Scrapy 中的 Selenium + PhantomJS

python - pylons + authkit 可以忽略具有 401 状态的特定响应吗?

Python 运行时错误(SyntaxErrorException): default value must be specified here

javascript - 如何通过 Python 使用 GhostDriver 处理警报?

python - 如何根据Python中行值的条件应用列中的值

python - XPath:选择具有空值的标记