I am trying to create a class that can be given any object on construction and then will list all of that object's callable methods in an interactive prompt. I am using the PyInquirer
module for the interactive prompt, and the inspect
module to get all the methods of the given object.
I have so far succeeded in dynamically building the prompt for any given object, but my program gives the error Foo() takes 0 positional arguments but 1 was given
when attempting to call one of the methods from the prompt.
I think the reason it crashes is that at the time of execution there are no more references to the object, so its reference count is at zero and the object is freed.
If I could get the class to keep hold of its own reference to the object, then this will solve the problem. For example
def __init__(self, object):
# Create a local reference the object
self.__object = &object
But this is invalid python.
How do I get a reference to the object (or manually increment the reference count, and manually decrement it in the __del__
function)?
Full source code for ObjectMethodTerminal.py can be found here https://gitlab.com/snippets/1939696
Change your sample class to this:
class A:
def Foo(self):
print("Hello")
def Bar(self):
print("World")
def Baz(self):
print("!")
Note the added self
to the method parameters.
If you had tried this code first:
a = A()
a.Foo()
You would have found your error before going the long route of inspecting the class.