How to view the source code of built-in functions in python

for example, how to view the source code of python built-in functions

give an example:

-sharp xrang
for i in xrange(10):
  print i

-sharp 
-sharp  pass
class xrange(object):
    """
    xrange(stop) -> xrange object
    xrange(start, stop[, step]) -> xrange object
    
    Like range(), but instead of returning a list, returns an object that
    generates the numbers in the range on demand.  For looping, this is 
    slightly faster than range() and more memory efficient.
    """
    def __getattribute__(self, name): -sharp real signature unknown; restored from __doc__
        """ x.__getattribute__("name") <==> x.name """
        pass

    def __getitem__(self, y): -sharp real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __init__(self, stop): -sharp real signature unknown; restored from __doc__
        pass

    def __iter__(self): -sharp real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): -sharp real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    @staticmethod -sharp known case of __new__
    def __new__(S, *more): -sharp real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __reduce__(self, *args, **kwargs): -sharp real signature unknown
        pass

    def __repr__(self): -sharp real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __reversed__(self, *args, **kwargs): -sharp real signature unknown
        """ Returns a reverse iterator. """
        pass

the libraries written by Python can be seen directly in the Python installation directory, while many built-in functions are written in C language, depending on the C language source code


the underlying layer is implemented in C language. Refer to the official source code
python/cpython/blob/master/Objects/rangeobject.c" rel=" nofollow noreferrer "> rangeobject.c
python/cpython/blob/master/Objects/iterobject.c" rel= "nofollow noreferrer" > iterobject.c

Menu