How can I get a class definition in a module?

Why can't I get the definition Callablefrom the module collectionsin the following code?

How can I get a class definition in a module? Thank.

>>> from collections import Callable
>>> inspect.getsource(Callable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/inspect.py", line 944, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python3.5/inspect.py", line 931, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.5/inspect.py", line 788, in findsource
    raise OSError('could not find class definition')
OSError: could not find class definition
>>> inspect.getsourcelines(Callable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/inspect.py", line 931, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.5/inspect.py", line 788, in findsource
    raise OSError('could not find class definition')
OSError: could not find class definition
>>> inspect.getmodule(Callable)
<module 'collections.abc' from '/usr/lib/python3.5/collections/abc.py'>
>>> inspect.getfile(Callable)
'/usr/lib/python3.5/collections/abc.py'
>>> inspect.getsourcefile(Callable)
'/usr/lib/python3.5/collections/abc.py'
+4
source share
2 answers

In general, this is easy to do with the help of a inspect.getsourcemodule, class, method, function, trace, frame, or code object. The source they represent must, of course, be written in Python, otherwise an error occurs.

In this particular case, you are just unlucky because when Callabledetermined in _collections_abc a nameCallable.__module__ callections.abc :

>>> Callable.__module__
'collections.abc'

getsource, _collections_abc, Callable, collections.abc, _collections_abc:

>>> print(getsource(collections.abc))
from _collections_abc import *
from _collections_abc import __all__

getsource , , :

>>> print(getsource(getsource))
def getsource(object):
    """Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    OSError is raised if the source code cannot be retrieved."""
    lines, lnum = getsourcelines(object)
    return ''.join(lines)

, , (- Callable.__module__ collections.abc.) __module__ '_collections_abc' :

>>> Callable.__module__ = '_collections_abc'
>>> src = getsource(Callable)  
>>> print(src)
class Callable(metaclass=ABCMeta):

    __slots__ = ()

    @abstractmethod
    def __call__(self, *args, **kwds):
        return False

    @classmethod
    def __subclasshook__(cls, C):
        if cls is Callable:
            return _check_methods(C, "__call__")
        return NotImplemented

.

+3

get_source(fullname) .

, , Callable _collections_abc, :

import _collections_abc

import inspect

print(inspect.getsource(_collections_abc))

and you can see the definition Callablein the print results. A.

+2
source

Source: https://habr.com/ru/post/1685823/


All Articles