Python AttributeError:类没有属性 [英] Python AttributeError: class has no attribute

查看:93
本文介绍了Python AttributeError:类没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有类队列的代码,该队列由 simpy Resource Container ( buffer )组成:

I have this code with a class queue which consist of simpy Resource and Container (buffer):

class queue:
    def __init__(self, env):
        self.port = simpy.Resource(env, capacity=1)
        self.buffer = simpy.Container(env, init = 0, capacity=1250000000)
        self.mon_proc = env.process(self.monitor_tank(env))

但是当我想使用该类及其使用的属性缓冲区时

But when I want to use the class and it's attribute buffer using

def Packet(env, id, size, port, time_in_port):

    arrive = env.now
    yield queue.buffer.put(size)
    print('packet%s %s arriving at %lf' % (id, size, arrive))

    with port.request() as req:
        yield req

        tip = random.expovariate(1/time_in_port)
        yield env.timeout(tip)
        amount = size
        yield queue.buffer.get(amount)
        print('packet%s %s depart at %lf' % (id, size, env.now))

调用queue.buffer时出现以下错误

I'm getting the following error when calling queue.buffer

AttributeError: class queue has no attribute 'buffer'

介意解释为什么我不能使用类中的属性?谢谢.

Mind to explain why I can't use the attribute from the class? Thanks.

推荐答案

如果 queue 是您的类,并且其实例属性为 buffer ,则可以访问 buffer 到类的 instances ,而不是类本身.

If queue is your class, and it has an instance attribute of buffer, then you can access buffer through instances of your class, not the class itself.

例如

class Queue:
    def __init__(self, env):
        self.port = simpy.Resource(env, capacity=1)
        self.buffer = simpy.Container(env, init = 0, capacity=1250000000)
        self.mon_proc = env.process(self.monitor_tank(env))

def Packet(env, id, size, port, time_in_port):
    queue = Queue(env) # instantiate your class
    ...
    # Make use of queue.buffer

这篇关于Python AttributeError:类没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆