1

I have been playing around with scripts for a couple of weeks and have not had any issues, but I am now trying to create a class and am running into problems.

I don't fully understand it myself but I am getting this error NameError: global name 'instance_status_check' is not defined when I try to create an instance of the following class.

I am fully aware the class isn't doing much at this time but I can't move on until I resolve the problem. Can someone explain what I am doing wrong?

import sys
import boto  
import boto.ec2 

class Monitor:

    def __init__(self,conn,identifier):
        self.connection = conn
        self.identifier = identifier
        self.dispatcher ={'1': instance_status_check}

    def user_menu():
        for i, value in self.dispatcher.itertems():
            print "Please press {i} for {value}".format(i,value)

    def instance_status_check():
        pass    
PhilipGough
  • 1,709
  • 1
  • 13
  • 18

2 Answers2

2

You are missing the self parameter from both methods and it is iteritems not itertems:

class Monitor:  # upper case for class names
    def __init__(self,conn,identifier):
        self.connection = conn
        self.identifier = identifier
        self.dispatcher ={'1': self.instance_status_check} # call self.instance_status_check()

    def user_menu(self): # self here
        for i, value in self.dispatcher.iteritems():
            print("Please press {i} for {value}".format(i,value))

    def instance_status_check(self): # self here
        return "In status method"

m = Monitor(3,4)
print(m.dispatcher["1"]())
In status method

I suggest you have a look at the classes tutorial from the docs

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • That works but was not what I was hoping to do with the dictionary value, I was hoping to build up a user menu and call for example dispatcher['1']() to call the instance_status_check function – PhilipGough Oct 20 '14 at 20:13
  • I edited to remove the parens, you should look at class methods if you want to share the dict among instances of the class – Padraic Cunningham Oct 20 '14 at 20:14
  • Thanks that works, can you explain why I need self. before a call to a function? Thanks – PhilipGough Oct 20 '14 at 20:22
  • `self` is used by convention, it can technically be anything but I would stick with self, it refers to the instance that the method is called on. There is more info in the link I posted, it covers a lot of the basics – Padraic Cunningham Oct 20 '14 at 20:24
0

You have this error because you have defined instance_status_check after you are already using it.

Move the declaration above the class:

def instance_status_check():
    pass 

class monitor:

    def __init__(self,conn,identifier):
        self.connection = conn
        self.identifier = identifier
        self.dispatcher ={'1': instance_status_check}

    def user_menu(self):
        for i, value in self.dispatcher.itertems():
            print "Please press {i} for {value}".format(i,value)

Also, this will not print Please press 1 for instance_status_check it will print something like Please press 1 for <function instance_status_check at 0xsomething>

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • So in normal circumstances, what I was doing would not be a problem? For example there was a different value for the dictionary entry? Thanks – PhilipGough Oct 20 '14 at 20:01