从类外部调用函数 - Python

时间:2015-04-24 16:07:54

标签: python function

stream_to_stop()内调用StreamingForexPrices时遇到问题。以下代码给出了此错误:

TypeError: unbound method stream_to_stop() must be called with StreamingForexPrices instance as first argument (got int instance instead)

有人可以帮我理解吗?感谢

class StreamingForexPrices(object):
    def __init__(
        self, domain, access_token,
        account_id, instruments, events_queue
    ):
        self.domain = domain
        self.access_token = access_token
        self.account_id = account_id
        self.instruments = instruments
        self.events_queue = events_queue

    def stream_to_stop(self):
        response = self.connect_to_stream()
        if response.status_code != 200:
            return
        for line in response.iter_lines(1):
            if line:
                try:
                    msg = json.loads(line)
                except Exception as e:
                    print "Caught exception when converting message into json\n" + str(e)
                    return
                if msg.has_key("instrument") or msg.has_key("tick"):
                    print msg["tick"]["ask"] - .001
                    instrument = msg["tick"]["instrument"]
                    time = msg["tick"]["time"]
                    bid = msg["tick"]["bid"]
                    ask = msg["tick"]["ask"]
                    stopLoss = msg["tick"]["ask"] - .001
                    tev = StopEvent(stopLoss)
                    self.events_queue.put(tev)

stop = StreamingForexPrices.stream_to_stop()
print stop

我的目标是打印stream_to_stop的输出..再次感谢!

编辑缩进..

1 个答案:

答案 0 :(得分:1)

domain = "www.example.com"
access_token = "ldkjflekfjelkxlk"
account_id = "account"
instruments = ["some instrument I don't how to play"]
events_queue = xxx # It sounds like an object created to handle queue
stop = StreamingForexPrices(domain, access_token, account_id, instruments, events_queue).stream_to_stop()

How to use python class