每个循环中的Python未定义函数

标签 python undefined

我是一个完整的 python n00b,我只是想把一些东西混在一起来使一个项目工作,但我正在为我认为的一些语法而苦苦挣扎。

这是我目前的脚本:

#!/usr/bin/env python

from plugin import *
from siriObjects.systemObjects import ResultCallback
import uuid
import json
import random
import types
import urllib
import urllib2
import random
import re
import select
import socket
import struct
import sys
import thread
import time

class tivoRemote(Plugin):

        tivo_address = '192.168.0.9'
        tivo_name = ''
        tivo_swversions = {}
        have_zc = True
        captions_on = False
        sock = None
        outer = None


        def connect():
            """ Connect to the TiVo within five seconds or report error. """
            global sock
            try:
                sock = socket.socket()
                sock.settimeout(5)
                sock.connect((tivo_address, 31339))
                sock.settimeout(None)
            except Exception, msg:
                msg = 'Could not connect to %s:\n%s' % (tivo_name, msg)
                print(msg)

        def send(message):
            """ The core output function, called from irsend(). Re-connect if
                necessary (including restarting the status_update thread), send
                message, sleep, and check for errors.
                """

            if not sock:
                self.connect()
                thread.start_new_thread(status_update, ())
            try:
                sock.sendall(message)
                time.sleep(0.1)
            except Exception, msg:
                error_window(str(msg))


        def irsend(*codes):
            """ Expand a command sequence for send(). """
            for each in codes:
                self.send('IRCODE %s\r' % each)


        @register("en-US", ".*Change.*Channel.*")
        def channelChanger(self, speech, language, matchedRegex):
                if language == 'en-US':
                        answer = self.ask(u"Which channel would you like?")
                        self.say(u"Ok, one moment..".format(answer))
                        self.connect()
                        self.irsend(answer)
                self.complete_request()

我得到的错误是:

Traceback (most recent call last):
  File "/home/pi/SiriServerCore/plugin.py", line 150, in run
    self.__method(self, self.__speech, self.__lang,        self.__method.__dict__[__criteria_key__][self.__lang].match(self.__speech))
  File "/home/pi/SiriServerCore/plugins/tivoRemote/__init__.py", line 70, in     channelChanger
    self.irsend(format(answer))
  File "/home/pi/SiriServerCore/plugins/tivoRemote/__init__.py", line 61, in irsend
    self.send('IRCODE %s\r' % each)
NameError: global name 'self' is not defined

如果我删除“ self ”。我得到同样的错误,但说“发送”未定义。

在此先感谢您的帮助:) 瑞安

最佳答案

更有可能工作:

class tivoRemote(Plugin):

    def __init__(self):
        self.tivo_address = '192.168.0.9'
        self.tivo_name = ''
        self.tivo_swversions = {}
        self.have_zc = True
        self.captions_on = False
        self.sock = None
        self.outer = None


    def connect(self):
        """ Connect to the TiVo within five seconds or report error. """
        try:
            sock = socket.socket()
            sock.settimeout(5)
            sock.connect((tivo_address, 31339))
            sock.settimeout(None)
        except Exception, msg:
            msg = 'Could not connect to %s:\n%s' % (tivo_name, msg)
            print(msg)
        self.sock = sock

    def send(self, message):
        """ The core output function, called from irsend(). Re-connect if
            necessary (including restarting the status_update thread), send
            message, sleep, and check for errors.
        """

        if not self.sock:
            self.connect()
            thread.start_new_thread(status_update, ()) # status_update must be some global at this point
        try:
            self.sock.sendall(message)
            time.sleep(0.1)
        except Exception, msg:
            error_window(str(msg))


    def irsend(self, *codes):
        """ Expand a command sequence for send(). """
        for each in codes:
            self.send('IRCODE %s\r' % each)


    @register("en-US", ".*Change.*Channel.*")
    def channelChanger(self, speech, language, matchedRegex):
            if language == 'en-US':
                    answer = self.ask(u"Which channel would you like?")
                    self.say(u"Ok, one moment..".format(answer))
                    self.connect()
                    self.irsend(answer)
            self.complete_request()

在定义方法时需要使用self,并且必须使用它来访问当前实例。

关于每个循环中的Python未定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11545046/

相关文章:

python - 在 Python 中使用自定义步骤生成列表序列

python - while 和 for 循环包含数学、列表、偶数、奇数、平方

python - 用 mako 创建一个表

javascript - 为什么 typeof undefined 是返回字符串?

javascript - AngularJS 路由参数

javascript - IE中输入值为 "undefined"

Python。如何删除数组中的特定键?

python - 字符串在 `eval` 语句中被视为变量

Javascript 根本不存储变量。每个变量 "undefined"

python - 模块未找到错误 : No module named 'pystan'