Nov-10-2019, 04:24 PM
Pages: 1 2
Nov-10-2019, 04:39 PM
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
Nov-20-2019, 12:38 PM
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?
Jan-19-2020, 02:41 PM
(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?
Jan-19-2020, 04:45 PM
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-22-2020, 04:00 PM
(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?
Jan-22-2020, 05:41 PM
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, 11:39 PM
(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 tryimport 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)
Jan-22-2020, 11:47 PM
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.
Jan-22-2020, 11:49 PM
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') FalseNow 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