How to check source code of a python method?

前端 未结 5 1619
遥遥无期
遥遥无期 2021-01-12 09:14

To be short, suppose I have a string \'next\',

next = \"123rpq\"

and I can apply a str method .isdigit()

相关标签:
5条回答
  • 2021-01-12 09:35

    You can try printing method.__doc__ for the documentation on that particular method. If you are just curious, you could check your PYTHONPATH to see where your modules are imported from, and see if you find the .py file from that module

    0 讨论(0)
  • 2021-01-12 09:40

    The isdigit() method you are talking about is a builtin method of a builtin datatype. Meaning, the source of this method is written in C, not Python. If you really wish to see the source code of it, then I suggest you go to http://python.org and download the source code of Python.

    0 讨论(0)
  • 2021-01-12 09:43

    I checked cpython source code, isdigit is already a c stdlib function. This explains it satisfactorily https://www.quora.com/How-does-isdigit-in-Python-work what you can do is download python source code and c stdlib header files and write a script (either bash or py) to search function (whatever type it is) and isdigit (whatever you search) in same line or close proximity.

    btw ,I wonder what would happen if we compile things part by part and associate code with aasembly. Would AI be able to shorten or expand our codes via comparing assemblies in a markov chain matter. AI assisted coding possible ?

    0 讨论(0)
  • 2021-01-12 09:56

    While I'd generally agree that inspect is a good answer, it falls flat when your class (and thus the class method) was defined in the interpreter.

    If you use dill.source.getsource from dill, you can get the source of functions and lambdas, even if they are defined interactively. It also can get the code for from bound or unbound class methods and functions defined in curries... however, you might not be able to compile that code without the enclosing object's code.

    >>> from dill.source import getsource
    >>> 
    >>> def add(x,y):
    ...   return x+y
    ... 
    >>> squared = lambda x:x**2
    >>> 
    >>> print getsource(add)
    def add(x,y):
      return x+y
    
    >>> print getsource(squared)
    squared = lambda x:x**2
    
    >>> 
    >>> class Foo(object):
    ...   def bar(self, x):
    ...     return x*x+x
    ... 
    >>> f = Foo()
    >>> 
    >>> print getsource(f.bar)
    def bar(self, x):
        return x*x+x
    
    >>> 
    

    For builtin functions or methods, dill.source will not work… HOWEVER…

    You still may not have to resort to using your favorite editor to open the file with the source code in it (as suggested in other answers). There is a new package called cinspect that purports to be able to view source for builtins.

    0 讨论(0)
  • 2021-01-12 09:58

    For modules, classes, functions and a few other objects you can use inspect.getfile or inspect.getsourcefile. However, for builtin objects and methods this will result in a TypeError. As metioned by C0deH4cker, builtin objects and methods are implemented in C, so you will have to browse the C source code. isdigit is a method of the builtin string object, which is implemented in the file stringobject.c in the Objects directory of the Python source code. This isdigits method is implemented from line 3392 of this file. See also my answer here to a similar but more general question.

    0 讨论(0)
提交回复
热议问题