Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在类中创建类,并在外部范围中创建实例?_Python_Class_Python Multiprocessing - Fatal编程技术网

Python 在类中创建类,并在外部范围中创建实例?

Python 在类中创建类,并在外部范围中创建实例?,python,class,python-multiprocessing,Python,Class,Python Multiprocessing,我试图用Python实现消费者-生产者问题……我的一个问题是,我是否可以在类中创建一个类,并在外部范围中创建它的对象,如下面的代码所示: class Main(threading.Thread): def __init__(self): processNumber = 0 queue_size=5 self.mutexProducer=thread.allocate_lock()#mutex variablaes self.

我试图用Python实现消费者-生产者问题……我的一个问题是,我是否可以在类中创建一个类,并在外部范围中创建它的对象,如下面的代码所示:

class Main(threading.Thread):
    def __init__(self):
        processNumber = 0
        queue_size=5
        self.mutexProducer=thread.allocate_lock()#mutex variablaes
        self.mutexConsumer=thread.allocate_lock()
        self.mutexTeller=thread.allocate_lock()
        self.queue=Queue.Queue(maxsize=queue_size)
        self.producer=Producer(processNumber,random.random())


class Producer(threading.Thread):
    def __int__(self,ProducerID,serviceTime):
        self.id=ProcucerID
        self.serviceTime=serviceTime
    def run(self):
            #mutexProducer.acquire()
            #Entering Critical section
        print queue.qsize()
        if queue.full():
            sleep(random.random())
        else:
            print "Customer %d Enters the Queue" %(self.id)


app=Main()
我得到以下错误:

Traceback(最近一次调用last):文件“/Users/sohil/Desktop/sync.py”,第55行,在
app=Main()文件“/Users/sohil/Desktop/sync.py”,第36行,在__
self.producer=producer(processNumber,random.random())name错误:未定义全局名称“producer”
更改顺序

class Producer(threading.Thread):
    def __int__(self,ProducerID,serviceTime):
        self.id=ProcucerID
        self.serviceTime=serviceTime
    def run(self):
            #mutexProducer.acquire()
            #Entering Critical section
        print queue.qsize()
        if queue.full():
            sleep(random.random())
        else:
            print "Customer %d Enters the Queue" %(self.id)

class Main(threading.Thread):
    def __init__(self):
        processNumber = 0
        queue_size=5
        self.mutexProducer=thread.allocate_lock()#mutex variablaes
        self.mutexConsumer=thread.allocate_lock()
        self.mutexTeller=thread.allocate_lock()
        self.queue=Queue.Queue(maxsize=queue_size)
        self.producer=Producer(processNumber,random.random())

Python是一种从上到下执行的解释语言,因此任何依赖项都必须在顶部声明。

Producer
类移到
Main
类之上。由于Python是解释的,而不是编译的,所以当执行
Main
中的代码行时,解释器还不知道
Producer
类。因此出现了错误。另外,还有一个输入错误,你的
\uuuuuu init\uuuuuu
方法名为
\uuuu int\uuuuu
@karthikr是的,我知道了。。。在Python中,我们创建了一些像C++一样的私有变量,在上面初始化构造函数的变量?关键是,解释v/s编译。虽然它们不是私有的,但您可以在
\uuuu init\uuuu
之外创建类属性。我是否遗漏了什么?在上面的代码中,类Producer与类Main处于相同的缩进级别,它不是嵌套的,并且在使用Main之前定义了Producer。事实上,我尝试了一个精简版本的代码,它的工作。你在这里打字时忘记缩进了吗?如果在Main中缩进Producer,那么它将成为Main类的一个属性,要在Main的
\uuuu init\uuuu
中引用它,您必须说Main.Producer.I可以在python中的类中创建类吗??所以我可以使用一些全局静态参数,比如队列?@ABC:只需在顶层声明这些变量。没有必要将它们包装在一个类中。如果需要封装,请将它们放在单独的模块中。@ABC您可以在另一个类的定义中创建一个类。在这里,你会发现很多不这样做的东西,但也有一些好的帖子/答案/例子。无论如何,如果你在一个类中创建了一个类,那么外部类中的其他东西对于内部类来说并不比外部类更容易访问——它仍然必须说OuterClass.thing@Kevin关于使用模块的看法是正确的。