Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Method
#1
How do I read a class file in order to discover the existence of a certain method ?
Reply
#2
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
Reply
#3
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?
Reply
#4
(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?
Reply
#5
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)
Reply
#6
(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?
Reply
#7
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.
Reply
#8
(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)
Reply
#9
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.
Reply
#10
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))
... 
Reply


Forum Jump:

User Panel Messages

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