0

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,

Ell Jeff
  • 49
  • 1
  • 8

2 Answers2

0

You're not actually calling the process_message function. To do that, you would need to run

process_message(some_variable)

Also, your function doesn't have a return statement. In Python, functions which end without a return statement will return None.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • Thanks for the help @DanielWalker, so if i wanted the function to return var1, i can place return var1 inside the function? How would i then get the return as an output? Also, i tried "process_message(var1)" but received "var1 is not defined". Sorry if i have the complete wrong end of the stick here... – Ell Jeff Jun 15 '20 at 04:46
  • No problem. You need to pass the message into `process_message`. That is, `process_message(msg)`. You can put `return var1` at the end of your function. Then you can access that value outside of the function by `var1 = process_message(msg)`. – Daniel Walker Jun 15 '20 at 15:57
  • Also, you should avoid fixing your code after someone posts an answer. For anyone reading in the future, it will confuse them. – Daniel Walker Jun 15 '20 at 15:59
  • Thanks @Dan, i was working on this all morning and i even created new unrelated functions and i get the behaviour you outlined above, which is what you'd expect. For whatever reason though, this particular function call always returns an error message. Either 'msg' is not defined, or returns NoneType value, despite it returning values from within the function. – Ell Jeff Jun 15 '20 at 17:49
  • Did you define `msg` before calling the function? – Daniel Walker Jun 16 '20 at 00:13
0

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.

Ell Jeff
  • 49
  • 1
  • 8