Python报错:需传入2个参数但仅传1个,代码调用问题排查
get_order_books参数不匹配的报错 Hey, let's break down why you're hitting that "Takes exactly 2 arguments 1 given" error and fix it up.
核心问题
The root cause is super straightforward: you're passing an extra argument 'prezels' to get_order_books, but the method's definition in the yellowgod library doesn't accept any parameters besides the implicit self (which Python automatically passes when you call an instance method).
Let's map this clearly:
- Your code:
price = client.get_order_books('prezels')— this sends 2 arguments to the method: the instance itself (self) and'prezels'. - The library's method definition:
def get_order_books(self):— this only expects 1 argument (self).
That's exactly why you're seeing the argument mismatch error.
修复方案
We have two paths depending on what you actually need:
1. 如果get_order_books不需要商品参数
If this method is designed to return all order books by default, just remove the extra argument when calling it:
price = client.get_order_books()
2. 如果确实需要获取'prezels'对应的订单簿
It looks like the library's get_order_books wrapper is missing support for passing a product filter, but the underlying _post2 method accepts **kwargs which lets you pass additional data to the API request. Here are two ways to handle this:
方案A:修改库的get_order_books方法(如果有权限编辑代码)
更新方法以接受商品参数,并把参数传递给_post2:
def get_order_books(self, symbol): # 把商品标识以JSON格式传给API请求 return self._post2('returnOrderBook', json={'symbol': symbol})
之后你就可以像原来那样调用:
price = client.get_order_books('prezels')
(注意:要确保symbol这个键名和API要求的一致,如果API用的是pair或其他术语,记得替换成对应名称)
方案B:直接调用_post2方法(如果无法修改库代码)
跳过get_order_books包装器,直接用底层方法发送请求:
price = client._post2('returnOrderBook', json={'symbol': 'prezels'})
这样就能绕过包装器,直接把参数传给API,应该就能拿到'prezels'的订单簿数据了。
内容的提问来源于stack exchange,提问作者DemonSlayer

