takes 1 positional argument but 2 were given; Combined String as Argument

Viewed 2365

i´m pretty new to python and even after searching the inet, i don´t understand what´s wrong here. For using the Binance API, i need the Command like this: client.get_order_book(symbol = 'ETHBTC')

As the symbol is dynamic, i tried

x1 = 'symbol = '
x2 = symbols[0].get('symbol')       #Output: ETHBTC
x = x1 + "'" + x2 + "'"
print(x)                            #Output: symbol = 'ETHBTC'

but when i call

client.get_order_book(x)

it draws the error:

TypeError: get_order_book() takes 1 positional argument but 2 were given

This is the rest of the Code:

client = Client(api_key, api_secret)

symbols = client.get_ticker()
print(symbols[0].get('symbol'))  #Output ETHBTC

y = len(symbols)
for i in range(y):
    x1 = 'symbol = '
    x2 = symbols[i].get('symbol')
    x = x1 + "'" + x2 + "'"
    print(x)  #Output: symbol = 'ETHBTC'
    print(client.get_order_book(x))

i read a lot about self, but i do not understand, where and how it has to be used here?!

2 Answers

x is the literal string symbol='ETHBTC', not a "saved" keyword argument. Your attempted call is equivalent to

client.get_order_book("symbol='ETHBTC'"),

but this method doesn't take any positional arguments (aside from self). Here's the signature:

def get_order_book(self, **params):

To prepackage keyword arguments and simulate

client.get_order_book(symbol='ETHBTC')

you need a dict to encapsulate the keyword argument.

x = {'symbol': 'ETHBTC'}
client.get_order_book(**x)

method get_order_book dont wait any parameters when it called. WHen you initiate instance of class by comand client = Client() self is gets into all not staticmethods by default as argument to refer to instance of class inside the method. You should point that method get_order_book gets a parameter:

class Client(object):
    ...
    def get_order_book(self, symbol):
        # do something with symbol

or if you don't need refer to instance of class in method:

class Client(object):
    ...
    @staticmethod
    def get_order_book(symbol):
        # do something with symbol

and if you want to args in method be predefined (vals by default) you should use:

...
def get_order_book(self, symbol="some_default_symbol"):
    ...
    print(symbol)    # will print "some_default_symbol"
Related