Python找不到类属性



为什么Factory.rdyq对继承的类不可见?

bar.py

import Queue
import threading

class Factory:
    name = ""
    rdyQ = Queue.Queue()
    dispQ = Queue.Queue()
    def __init__(self):
        self.worker_count = 1
        self.worker_thread = None
    def worker(self): pass
    def initialize(self):
        for i in range(self.worker_count):
            t = threading.Thread(target=self.worker)
            t.daemon = True
            t.start()
            self.worker_thread = t


    @staticmethod
    def joinQ():
        Factory.rdyQ.join()
        Factory.dispQ.join()
        return
    @staticmethod
    def getFactory(factory_name):
        if factory_name == "setup":
            return SetupFactory()
        elif factory_name == "dispatch":
            return DispatchFactory()
        elif factory_name == "complete":
            return CompleteFactory()
        else:
            return None
class SetupFactory(Factory):
    name = "setup"
    def worker(self):
        while True:
            try:
                item = Factory.rdyq.get(timeout=1)
                print "setup: %s" % item
                Factory.rdyq.task_done()
                Factory.dispQ.put(item)
            except Queue.Empty, msg:
                continue
class DispatchFactory(Factory):
    name = "dispatch"
    def worker(self):
        while True:
            try:
                item = Factory.dispQ.get(timeout=1)
                print "dispQ: %s" % item
                Factory.dispQ.task_done()
            except Queue.Empty, msg:
                continue
class CompleteFactory(Factory):
    name = "complete"
    def worker(self):
        for i in range(10):
            Factory.rdyq.put(i)

foo.py

import bar as df
setup = df.Factory.getFactory("setup")
dispatch = df.Factory.getFactory("dispatch")
complete = df.Factory.getFactory("complete")
setup.initialize()
dispatch.initialize()
complete.initialize()
df.Factory.joinQ()
setup.worker_thread.join()
dispatch.worker_thread.join()
complete.worker_thread.join()
python foo.py

  File "/u/bar.py", line 73, in worker
    Factory.rdyq.put(i)
AttributeError: class Factory has no attribute 'rdyq'

joinQ应该使用classmethod而不是staticmethod

你也有一个rdyq (bar.py最后一行)而不是预期的rdyQ, Python是区分大小写的。

bar.py

import Queue
import threading

class Factory:
    name = ""
    rdyQ = Queue.Queue()
    dispQ = Queue.Queue()
    def __init__(self):
        self.worker_count = 1
        self.worker_thread = None
    def worker(self): pass
    def initialize(self):
        for i in range(self.worker_count):
            t = threading.Thread(target=self.worker)
            t.daemon = True
            t.start()
            self.worker_thread = t


    @classmethod
    def joinQ(cls):
        cls.rdyQ.join()
        cls.dispQ.join()
        return
    @staticmethod
    def getFactory(factory_name):
        if factory_name == "setup":
            return SetupFactory()
        elif factory_name == "dispatch":
            return DispatchFactory()
        elif factory_name == "complete":
            return CompleteFactory()
        else:
            return None
class SetupFactory(Factory):
    name = "setup"
    def worker(self):
        while True:
            try:
                item = Factory.rdyq.get(timeout=1)
                print "setup: %s" % item
                Factory.rdyq.task_done()
                Factory.dispQ.put(item)
            except Queue.Empty, msg:
                continue
class DispatchFactory(Factory):
    name = "dispatch"
    def worker(self):
        while True:
            try:
                item = Factory.dispQ.get(timeout=1)
                print "dispQ: %s" % item
                Factory.dispQ.task_done()
            except Queue.Empty, msg:
                continue
class CompleteFactory(Factory):
    name = "complete"
    def worker(self):
        for i in range(10):
            Factory.rdyQ.put(i)

Python变量和属性是区分大小写的,所以"rdyq"one_answers"rdyq"是不一样的。

您将名称设置为大写q,所以也许这将解决它?

Factory.rdyQ.put (i)

我建议使用ing_underscores_for_variables以避免camelCase可能引起的问题。

相关内容

  • 没有找到相关文章

最新更新