python - 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
解决方案
首先你必须创建这个类的实例
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)
推荐阅读
- java - 为什么我的小数点后面只有一位而不是两位?
- function - 是否有可以对时间框架的烛台进行编号的函数或代码?例如:4H = 蜡烛 1,2,3,4
- authentication - 在访问父应用程序管道之前 IIS 服务子应用程序
- c - 无法从特定文件夹创建文件的 tar 文件。提取后路径错误
- javascript - Vuejs otp 下一个输入
- html - 当我单击它时,Jquery 无法隐藏子菜单
- flutter - toggleFold() 方法不存在
- javascript - 为什么在这个背景下重复布局图像
- variables - Grafana 和日期变量下拉菜单
- php - 设置对象的属性会覆盖相同类型对象中的该属性