1

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.

asked Feb 3, 2016 at 5:36

1 Answer 1

1
self.Mycode = { 5:self.func1 }
 ^^^^

It should be self.

Also,

temp = self.Mycode[indx1]
 ^^^^
answered Feb 3, 2016 at 5:38
Sign up to request clarification or add additional context in comments.

1 Comment

Thank-you very much!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.