鍍金池/ 問(wèn)答/Python/ python如何查看內(nèi)置函數(shù)源碼

python如何查看內(nèi)置函數(shù)源碼

如題,如何查看python內(nèi)置函數(shù)的源碼

舉個(gè)例子:

# 該以什么樣的方式,才能去看xrang的官方源碼實(shí)現(xiàn)
for i in xrange(10):
  print i

# 直接點(diǎn)進(jìn)去的話,大多都是一些定義的方法名,而沒(méi)有具體實(shí)現(xiàn)
# 就是想知道,如何去看具體實(shí)現(xiàn),而不是下面那一堆 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): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

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

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

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

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

    @staticmethod # known case of __new__
    def __new__(S, *more): # 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): # real signature unknown
        pass

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

    def __reversed__(self, *args, **kwargs): # real signature unknown
        """ Returns a reverse iterator. """
        pass
回答
編輯回答
笑忘初

一般由Python寫(xiě)出來(lái)的庫(kù)直接可以在Python安裝目錄下看到,而很多內(nèi)置的函數(shù)使用C語(yǔ)言寫(xiě)出來(lái)的,要看C語(yǔ)言的源代碼

2017年7月23日 09:55
編輯回答
壞脾滊

底層是C語(yǔ)言實(shí)現(xiàn)的,實(shí)現(xiàn)參考官方源碼:
rangeobject.c
iterobject.c

2018年7月21日 07:26