2
File "clienttest.py", line 3, in <module>
   x = handle.Client()
File "/home/tmac/Handle/handle.py", line 131, in __init__
   Task.CLT_UPDATE:self.__clt_updates,
AttributeError: 'Client' object has no attribute '_Client__clt_updates'

我的代码在创建实例时抛出此错误,但该函数存在。它似乎在它前面加上 _Client,我不知道为什么

这是类定义

class Client(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        #create task q
        self.tasks = Queue.Queue(maxsize=0)
        print str(self)
        #create database
        self.database = server.Database()
        self.database.loadconfig()

        #create networking
        self.network = network.Network(self.tasks)

        #put startup tasks
        self.network.cmd_q.put(NetworkCommand(NetworkCommand.CONNECT,('127.0.0.1',self.database.config['Handle']['port'])))

        #create gui
        self.gui = gui.Gui(self)
        #define task handlers
        self.handlers = {
                Task.CLT_UPDATE:self.__clt_updates,
                            ^----The problem seems to be here--------^
                Task.CLT_INPUT:self.__clt_input,
                Task.CLT_LINEUP:self.__clt_lineup
                }

        #define alive flag
        self.alive = threading.Event()
        self.alive.set()

        #setup logging
        logging.basicConfig(level=self.loglvl, filename='client.log', format='%(asctime)s %(name)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
        self.log = logging.getLogger('CLIENT')



        def run(self):
            self.network.start()
            self.gui.initscreen()
            while self.alive.isSet():
                try:
                    task = self.tasks.get(True, 0.1)
                    self.log.debug('Got Task: ' + task.stype[task.type])
                    self.handlers[task.type](task)

                except Queue.Empty:
                    pass


        def addtask(self, task):
            self.tasks.put(task)

        def __clt_updates(self, task):
            raise NotImplemented()

        def __clt_input(self, task):
            pack = Packet(Packet.INPUT,task.data)
            self.network.cmd_q.put(NetworkCommand(NetworkCommand.SEND,pack))
            self.log.debug(':[H]Sent Command: ' + task.data)

        def __clt_lineup(self, task):
            self.gui.addline(task.data)
            self.log.debug(':[H]Line Update: ' + task.data)
4

1 回答 1

4

Python 中的双下划线会导致 Python名称重整在运行时生效。名称修改会将类名添加到类中名称以 2 个下划线开头的每个方法之前。如果您想要没有名称修饰的私有方法,请使用单个下划线。

如评论中所述,您似乎也有缩进问题。我的猜测是您打算将这些方法创建为类成员,但是由于问题在这里有它们,它们实际上缩进在__init__.

于 2012-04-27T22:15:37.797 回答