1

Hi, I'm looking for a little help with the function "getattr". Here's my code:

class MyClass:
      def __init__(self):
            trycmd = getattr(self, "cmd_testing")
            try:
                  trycmd()
            except:
                  print "Fail."
                  
      def cmd_testing(self):
            print "Victory!"
            
obj = MyClass()

So, when I run this code, I get the output "Fail.". I'm not sure what I'm doing wrong, but as I have tested a little more, I have come to the conclusion that it is not finding the right method (cmd_testing). How can I make this work?

Thanks in advance.

EDIT: My bad, I found out I did a really stupid mistake. I forgot the "None" argument when I set the "trycmd" variable. Problem resolved.

Edited by SoulMazer: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by vegaseat
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.