繁体   English   中英

从类外部调用函数-Python

[英]Call a function from outside a class - Python

StreamingForexPrices内调用stream_to_stop()遇到一些麻烦。以下代码给出此错误:

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的输出。再次感谢!

编辑缩进

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()

如何使用python类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM