Dec-12-2017, 07:46 PM
I'll try to communicate this as clearly as possible. I am still fairly new. Thanks in advance!
Here is my issue, in a nutshell:
I have created a custom class with methods and instantiated it successfully.
I have created a list containing these instances.
When I call the method explicitly by referencing objects in my list by index, everything works fine.
When I store the method in a variable and try to apply it the same way I get an error ("name 'describe' is not defined").
Ultimately, I am trying to pass this user created method as an argument of a function, but the result is the same error as above.
Below is the pseudocode, which I think is the best way to communicate my problem.
All help is appreciated. I will clarify as best as I can. Thanks for your patience, I am trying to digest quite a few concepts here.
# Create custom class
class MyClass:
def constructor():
statement
statement
def my_method():
statment
statement
# Create some instances of custom class
item1 = MyClass()
item2 = MyClass()
item3 = MyClass()
# Create a list containing the instances
my_list = [item1, item2, item2]
[# This works perfectly:
result = my_list[index].my_method()
# This does not work:
# (Error = name 'my_method' is not defined)
m = my_method()
result = my_list[index].m
# This throws the same error:
my_function(my_list, my_method)
# However, the following works exactly as desired (obviously I adjust the parameters in the function definition according to which approach I am trying):
my_function(my_list).my_method()
Here is my issue, in a nutshell:
I have created a custom class with methods and instantiated it successfully.
I have created a list containing these instances.
When I call the method explicitly by referencing objects in my list by index, everything works fine.
When I store the method in a variable and try to apply it the same way I get an error ("name 'describe' is not defined").
Ultimately, I am trying to pass this user created method as an argument of a function, but the result is the same error as above.
Below is the pseudocode, which I think is the best way to communicate my problem.
All help is appreciated. I will clarify as best as I can. Thanks for your patience, I am trying to digest quite a few concepts here.
# Create custom class
class MyClass:
def constructor():
statement
statement
def my_method():
statment
statement
# Create some instances of custom class
item1 = MyClass()
item2 = MyClass()
item3 = MyClass()
# Create a list containing the instances
my_list = [item1, item2, item2]
[# This works perfectly:
result = my_list[index].my_method()
# This does not work:
# (Error = name 'my_method' is not defined)
m = my_method()
result = my_list[index].m
# This throws the same error:
my_function(my_list, my_method)
# However, the following works exactly as desired (obviously I adjust the parameters in the function definition according to which approach I am trying):
my_function(my_list).my_method()