Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Method
#11
(Nov-10-2019, 04:24 PM)JohnnyCoffee Wrote: How do I read a class file in order to discover the existence of a certain method ?

I don't know whether it addresses the problem but if I want to 'discover' all the methods of str class then in interactive interpreter I just:

>>> str.    # 2 x TAB
str.capitalize(    str.find(          str.isdecimal(     str.istitle(       str.mro(           str.rsplit(        str.title(
str.casefold(      str.format(        str.isdigit(       str.isupper(       str.partition(     str.rstrip(        str.translate(
str.center(        str.format_map(    str.isidentifier(  str.join(          str.replace(       str.split(         str.upper(
str.count(         str.index(         str.islower(       str.ljust(         str.rfind(         str.splitlines(    str.zfill(
str.encode(        str.isalnum(       str.isnumeric(     str.lower(         str.rindex(        str.startswith(    
str.endswith(      str.isalpha(       str.isprintable(   str.lstrip(        str.rjust(         str.strip(         
str.expandtabs(    str.isascii(       str.isspace(       str.maketrans(     str.rpartition(    str.swapcase(    
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#12
(Jan-22-2020, 11:49 PM)Gribouillis Wrote: 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)) ... 

I managed only using the Built-in:
hasattr(object, name)

(Jan-22-2020, 11:49 PM)Gribouillis Wrote: 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)) ... 

consegui apenas usando o Built-in :
hasattr(object, name)
Reply


Forum Jump:

User Panel Messages

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