I wrote this code to access an object via user input. It works with locals (I know I can make dictionary too)
class Dragon:
def __init__(self, head, legs):
self.head = head
self.legs = legs
def sum(self):
return self.head + self.legs
redfire = Dragon(2, 4)
dic1 = {"redfire": redfire}
input_object = input()
print(dic1[input_object].sum())
but when I'm trying to do the same with methods of the class:
class Dragon:
def __init__(self, head, legs):
self.head = head
self.legs = legs
def sum(self):
return self.head + self.legs
def mul(self):
return self.head * self.legs
redfire = Dragon(2, 4)
dic1 = {"redfire": redfire, "sum": Dragon.sum(self), "mul": Dragon.mul}
input_object = input()
input_method = input()
print(dic1[input_object].dic1[input_method])
I'm getting all kinds of errors asking me to modify my dictionary:
Traceback (most recent call last):
File "C:\Users\millw0rm\test.py", line 10, in <module>
dic1 = {"redfire": redfire, "sum": Dragon.sum(self), "mul": Dragon.mul}
NameError: name 'self' is not defined
What can I do to define a valid key for my methods in my dictionary?