Search code examples
pythonfunctionwebsocketbinance

Easy question regarding how to access the return of a function


I'm new to python/programming so this may be a simple solution. I'm just trying to figure out how to output the function?

I use the following:

import time
from binance.client import Client
from datetime import datetime

def process_message(msg):
    print("message type: {}".format(msg['e']))
    print(msg)
    var1 = msg['s']
    var2 = msg['p']
    print(var1 + var2)
    return(var1)

from binance.websockets import BinanceSocketManager
bm = BinanceSocketManager(client)
bm.start_trade_socket('BNBBTC', process_message)
bm.start()

At this point, the websocket starts streaming data as expected.

So i can see the results of the function if i call it from inside the function, but i receive an error if i try and call it like this (outside of the function):

print(process_message)

I receive the following: function process_message at 0x03A919B8

If i call the function on it's own:

process_message()

I receive: "process_message() missing 1 required positional argument: 'msg'"

If i call the function with the argument:

process_message(msg)

I get: name 'msg' is not defined

What am i doing wrong? How would i go about accessing the data outside of the function?

Any help or clarity would be appreciated,

Many Thanks,


Solution

  • So the reason is doesn't work as expected, is because in short, i was wrong :)

    The function listed above is a callback function, it's entire job is to process the message received by another function, so in order to get the outcome i desired, i needed to amend the original function, and not the callback function.

    Will scratch this one up to not knowing enough about functions before i posted the question.

    Thank you for the time.