Python Forum

Full Version: Read Method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do I read a class file in order to discover the existence of a certain method ?
This is not Java, there are no class files. If you want the help of class Spam if file eggs.py, you can do this
>>> import eggs
>>> help(eggs.Spam)
You could also type in an ordinary terminal (outside of python)
Output:
pydoc eggs.Spam
Hello! Probably you mixed something up, as no such a thing as "a class file" exists in Python... Perhaps you're looking for something else?
(Nov-10-2019, 04:39 PM)Gribouillis Wrote: [ -> ]This is not Java, there are no class files. If you want the help of class Spam if file eggs.py, you can do this
 >>> import eggs >>> help(eggs.Spam) 
You could also type in an ordinary terminal (outside of python)
Output:
pydoc eggs.Spam

What I need to find out if it is possible to know is whether a method of a particular class exists?
JohnnyCoffee Wrote:What I need to find out if it is possible to know is whether a method of a particular class exists?
Try this for example
>>> class A:
...     def foo(self): pass
...     def bar(self): pass
... 
>>> help(A)
Output:
class A(builtins.object) | Methods defined here: | | bar(self) | | foo(self) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
(Jan-19-2020, 04:45 PM)Gribouillis Wrote: [ -> ]
JohnnyCoffee Wrote:What I need to find out if it is possible to know is whether a method of a particular class exists?
Try this for example
 >>> class A: ... def foo(self): pass ... def bar(self): pass ... >>> help(A) 
Output:
class A(builtins.object) | Methods defined here: | | bar(self) | | foo(self) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)

Okay, is there any way to access the class file and see if a certain method exists?
JohnnyCoffee Wrote:Okay, is there any way to access the class file and see if a certain method exists?
You can try
import inspect
f = inspect.getfile(A)
s = inspect.getsource(A)
that kind of things.
(Jan-22-2020, 05:41 PM)Gribouillis Wrote: [ -> ]
JohnnyCoffee Wrote:Okay, is there any way to access the class file and see if a certain method exists?
You can try
 import inspect f = inspect.getfile(A) s = inspect.getsource(A) 
that kind of things.

I set up the example below that returns me (True) if the 'a' method exists :
import inspect

class A:
    def a():
        pass

exist = A()
s = inspect.ismethod(exist.a)
print(s)
But simulating the access to a method there is no 'b' and it returns an error message: AttributeError: 'A' object has no attribute 'b'. Would have to return the boolean (False)
Python doesn't have macros, so the stuff in the parenthesis is evaluated prior to ismethod being invoked. A reasonable workaround would be to catch the AttributeError, and treat that as False.
Because inspect.ismethod() doesn't do what you are expecting. You could use something like
>>> from types import FunctionType
>>> class A:
...     def a(self):
...         pass
... 
>>> 
>>> def hasmethod(cls, attr):
...     return hasattr(A, attr) and isinstance(getattr(A, attr), FunctionType)
... 
>>> hasmethod(A, 'a')
True
>>> hasmethod(A, 'b')
False
Now it depends on what you call a method, you could also try
>>> def hasmethod(cls, attr):
...     return callable(getattr(A, attr, None))
... 
Pages: 1 2