首页 > 解决方案 > Cmd 模块不与 *args 合作

问题描述

我正在尝试使用 Cmd 模块开发我的 Binance 客户端,但是当我尝试从另一个文件调用函数时,它给了我一个错误。

这是给我正在写的 Binance 客户的。

主文件

def do_order(self, *args):
    """Places an order.\n
    Arguments in order:\n
    Buy or Sell option.\n
    Market or Limit option.\n
    The crypto symbol.\n
    The quantity of the order.\n
    The price of the order.\n"""



    market.Market.xch(*args[0], *args[1], *args[2], *args[3], *args[4])

市场.py

class Market():
    #l = 0
    #m = 0
    #b = 0
    #s = 0

    def xch(self, xtype1, xtype2, xsymbol, xquantity, xprice ):

    print("Formulating the order...")

    #Time to sort the parameters

    #xtype1...

错误

[verl@verlpc Interface]$ python main.py
Loading Accounts...
CRYPTOANAYLISIS: PREDICTOR
> order 0 0 0 0 0
Traceback (most recent call last):
  File "main.py", line 99, in <module>
    m.initAccounts()
  File "main.py", line 92, in initAccounts
    prompt.cmdloop('CRYPTOANAYLISIS: PREDICTOR')
  File "/usr/lib/python3.7/cmd.py", line 138, in cmdloop
    stop = self.onecmd(line)
  File "/usr/lib/python3.7/cmd.py", line 217, in onecmd
    return func(arg)
  File "main.py", line 50, in do_order
    market.Market.xch(*args[0], *args[1], *args[2], *args[3], *args[4])
IndexError: tuple index out of range

标签: python

解决方案


首先你必须创建这个类的实例

  m = market.Market()

然后你使用了单*args

  m.xch(*args)

或许多论点,但没有*

  m.xch(args[0], args[1], args[2], args[3], args[4])

工作示例

class Market():
    def xch(self, xtype1, xtype2, xsymbol, xquantity, xprice ):
        print("Formulating the order...", xtype1, xtype2, xsymbol, xquantity, xprice)

args = [1,2,3,4,5]
m = Market()
m.xch(*args)

编辑:你必须使用do_order(...)

*定义中以及当你运行它时

def do_order(*args):
    m = Market()
    m.xch(*args)    

args = [1,2,3,4,5]
do_order(*args)

*在两个地方都没有

def do_order(args):
    m = Market()
    m.xch(*args)    

args = [1,2,3,4,5]
do_order(args)

推荐阅读