-1

I want to be able to create an instance of a class, and call methods on this instance which will connect me to a service (SQS, if that matters). I then want to be able to call methods which will let me interact with that service.

For some reason, I keep getting this error on import:

    class AmazonQueue(queue_id):
NameError: name 'queue_id' is not defined

But as far as I can tell, this isn't even getting called at import, so I shouldn't get this error.

Inside AmazonQueue is this method:

def __new__(self, queue_id):
        conn = am_auth.BasicAuth()
        q = conn.create_queue(queue_id)
        return q

Which just returns an object representing authentication to the service, which I think SHOULD theoretically be available for other methods to run.

In my script I wanted this to run like this:

q = am_queue.AmazonQueue(queue_id='queue_id_goes_here')

and then have a method called inside AmazonQueue() like so:

q.post_message('blahblahblah')

So, why am I getting this error if I'm not even instantiating the class on import?

Steven Matthews
  • 9,705
  • 45
  • 126
  • 232

1 Answers1

0

In your class definition the paramaters to AmazonQueue are the parent classes, not the parameters you would like to require for instantiation

One option could be to just create a function since your example use is pretty straightforward

def get_queue(queue_id):
  conn = am_auth.BasicAuth()
  q = conn.create_queue(queue_id)
  return q

One thing to consider is:

In general, you shouldn't need to override new unless you're subclassing an immutable type like str, int, unicode or tuple.

From Python's use of __new__ and __init__? answer on stack overflow

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145