Python Forum
User defined method as variable or argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User defined method as variable or argument
#5
You must fix the end of the script a little bit to get it work:

# Define simple function that takes an object and method as arguments
def my_function(the_object, the_method):
    getattr(the_object, the_method)()


# Call the function with args
my_function(my_obj, "describe")
You called my_function with the describe variable which hasn't been defined. And inside my_function you called the method also bad. You must get the object method with getattr(object, name_of_the_method) and then call it with () - so getattr(object, name_of_the_method)()
Reply


Messages In This Thread
RE: User defined method as variable or argument - by ODIS - Dec-13-2017, 01:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 355 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 633 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,406 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,143 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,954 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  User-defined function to reset variables? Mark17 3 1,699 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,499 Apr-30-2022, 10:19 AM
Last Post: maltp
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,200 Apr-05-2022, 04:55 AM
Last Post: deanhystad
Question How to pass a method as argument in an another method? anilanvesh 6 2,800 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,242 Apr-20-2021, 11:15 AM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020