Reputation: 69
I am trying to create a 'switch' statement using the dictionary method. I can do this without classes, but with classes I can't seem to make it work no matter what type of incantation I try.
#!/usr/bin/python3
import numpy as np
import sys
def func1(var1):
print("hi guy")
return 0
Mycode = { 5:func1 }
indx1 = 5
var1 = 0
temp = Mycode[indx1]
temp(var1)
sys.exit()
The above code works.
The code below does not work
#!/usr/bin/python3
import numpy as np
import sys
class Beef():
def __init__(self):
junk = 3
def func1(self, var1):
print("hi guy")
return 0
def find(self):
self.Mycode = { 5:func1 }
self.run()
def run(self):
indx1 = 5
var1 = 0
temp = self.Mycode[indx1]
errcode = temp(0)
sys.exit()
hvfbeef = Beef()
hvfbeef.find()
I get the error "NameError: name 'func1' is not defined" , although upon other permutations I get other errors. I seems the I don't understand how the dictionary works within classes and functions. Any help would be appreciated.
Upvotes: 1
Views: 67
Reputation: 67968
self.Mycode = { 5:self.func1 }
^^^^
It should be self.
Also,
temp = self.Mycode[indx1]
^^^^
Upvotes: 1