Read Method - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Read Method (/thread-22372.html) Pages:
1
2
|
Read Method - JohnnyCoffee - Nov-10-2019 How do I read a class file in order to discover the existence of a certain method ? RE: Read Method - Gribouillis - Nov-10-2019 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)
RE: Read Method - ChislaineWijdeven - Nov-20-2019 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? RE: Read Method - JohnnyCoffee - Jan-19-2020 (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) What I need to find out if it is possible to know is whether a method of a particular class exists? RE: Read Method - Gribouillis - Jan-19-2020 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)
RE: Read Method - JohnnyCoffee - Jan-22-2020 (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) Okay, is there any way to access the class file and see if a certain method exists? RE: Read Method - Gribouillis - Jan-22-2020 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. RE: Read Method - JohnnyCoffee - Jan-22-2020 (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) RE: Read Method - micseydel - Jan-22-2020 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. RE: Read Method - Gribouillis - Jan-22-2020 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)) ... |