(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(
(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)