Python สามารถพิมพ์นิยามฟังก์ชันได้หรือไม่?


114

ใน JavaScript เราสามารถพิมพ์นิยามของฟังก์ชันได้ มีวิธีทำให้สำเร็จใน Python หรือไม่?

(แค่เล่นในโหมดโต้ตอบและฉันต้องการอ่านโมดูลโดยไม่ต้องเปิด () ฉันแค่อยากรู้)


คุณมีแหล่งที่มาของฟังก์ชัน มันผิดอะไร?
ล็อ

1
และจากโหมดโต้ตอบคุณสามารถใช้ help (ฟังก์ชัน) เพื่อแสดง docstring สำหรับฟังก์ชัน
กุฏ

มีคำถามซ้ำกัน: stackoverflow.com/questions/427453/…
Anderson Green

คำตอบ:


158

หากคุณกำลังนำเข้าฟังก์ชันคุณสามารถใช้inspect.getsource:

>>> import re
>>> import inspect
>>> print inspect.getsource(re.compile)
def compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a pattern object."
    return _compile(pattern, flags)

สิ่งนี้จะทำงานในพรอมต์แบบโต้ตอบ แต่เห็นได้ชัดเฉพาะบนวัตถุที่นำเข้า (ไม่ใช่วัตถุที่กำหนดไว้ในพรอมต์แบบโต้ตอบ) และแน่นอนว่าจะใช้งานได้ก็ต่อเมื่อ Python สามารถค้นหาซอร์สโค้ดได้ (ดังนั้นไม่ใช่ในอ็อบเจ็กต์ในตัว, C libs, ไฟล์. pyc ฯลฯ )


ฟังก์ชั่นที่สร้างขึ้นในรันไทม์ (รวมถึงพรอมต์แบบโต้ตอบ) ไม่มีไฟล์หรือหมายเลขลินินด้วยซึ่งสมเหตุสมผล
John La Rooy

นั่นดูเหมือนจะเป็นสิ่งที่ฉันกำลังมองหา ขอบคุณ!
Eddie Welker

9
สิ่งที่เกี่ยวกับการพิมพ์นิยามฟังก์ชันที่ฉันกำหนดไว้ก่อนหน้านี้ในล่าม Python แบบโต้ตอบปัจจุบัน เป็นไปได้หรือไม่
GL2014

@ GL2014: ใช่ดูคำตอบของฉัน
Mike McKerns

เราสามารถยืนยันได้ว่าคำตอบนี้และ inspect.getsource () ไมทำงานเกี่ยวกับการโต้ตอบ (ipython3) funciton กำหนดไว้ใน Python 3.6.9 (Ubuntu)
Gnudiff

97

หากคุณใช้iPythonคุณสามารถใช้function_name?เพื่อขอความช่วยเหลือและfunction_name??จะพิมพ์แหล่งที่มาหากทำได้


2
บางครั้งคุณต้องแยกฟังก์ชั่นเป็นสายอื่นเพื่อโทร? เช่น model.function ?? ไม่ทำงาน แต่ f = model.function; ฉ ?? งาน
thecheech

12

นี่คือวิธีที่ฉันคิดว่าจะทำอย่างไร:

    import inspect as i
    import sys
    sys.stdout.write(i.getsource(MyFunction))

สิ่งนี้จะนำอักขระบรรทัดใหม่ออกมาและพิมพ์ฟังก์ชันออกมาอย่างสวยงาม


1
ตัวอย่างเสียที่นั่นควรเป็น i.getsource (MyFunction)
svth

10

แม้ว่าโดยทั่วไปฉันจะยอมรับว่าinspectเป็นคำตอบที่ดี แต่ฉันไม่เห็นด้วยที่คุณไม่สามารถรับซอร์สโค้ดของวัตถุที่กำหนดไว้ในล่ามได้ หากคุณใช้dill.source.getsourceจากdillคุณจะได้รับแหล่งที่มาของฟังก์ชันและแลมบ์ดาแม้ว่าจะถูกกำหนดแบบโต้ตอบก็ตาม นอกจากนี้ยังสามารถรับรหัสจากเมธอดคลาสที่ถูกผูกไว้หรือไม่ถูกผูกไว้และฟังก์ชันที่กำหนดไว้ในแกง ... อย่างไรก็ตามคุณอาจไม่สามารถคอมไพล์โค้ดนั้นได้หากไม่มีโค้ดของอ็อบเจ็กต์ล้อมรอบ

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

>>> 

1
บางครั้งมันใช้ไม่ได้โดยตรงกับ: getsource (my_function)แต่จากนั้นฉันสามารถทำให้มันทำงานกับgetsource (my_function.func_code)
Afflatus

3

ใช้help(function)เพื่อรับคำอธิบายฟังก์ชัน

คุณสามารถอ่านเพิ่มเติมเกี่ยวกับที่นี่help()


1
ไม่มีแหล่งที่มาที่กำหนดไว้ในความช่วยเหลือสำหรับฉัน
ansuman

-6

คุณสามารถใช้คีย์เวิร์ด __doc__:

#print the class description
print string.__doc__
#print function description
print open.__doc__

1
นั่นคือคำอธิบายไม่ใช่คำจำกัดความ
Triptych

สำหรับบิวด์อินจำนวนมาก (ตามกฎคือฟังก์ชันที่กำหนดไว้ในโมดูล C) จะมีลายเซ็นฟังก์ชันด้วยเช่นกัน แต่ไม่ใช่โดยทั่วไป
u0b34a0f6ae

1
ในขณะที่อยู่ในเชลล์แบบโต้ตอบ "help (object)" จะแสดงสิ่งนี้ในรูปแบบการนำทางมากขึ้น
ทีเค.

@kaizer ลายเซ็นของฟังก์ชันไม่ใช่คำจำกัดความเช่นกัน สิ่งที่ส่งกลับ__doc__ จริงคือสิ่งที่ผู้เขียนโค้ดใส่ในสตริง doc (สตริงที่ยกมาสามตัว) ไม่มีอะไรมากไม่มีอะไรน้อย
Triptych

ฉันคิดว่าคำจำกัดความที่นี่คลุมเครือ สำหรับฉันมันอาจหมายถึง docstring หรือข้อความรหัสหรือทั้งสองอย่างหรือแม้แต่วัตถุรหัสที่ยืดออก
John La Rooy

-6

คุณสามารถใช้__doc__ในฟังก์ชันใช้hog()ฟังก์ชันเป็นตัวอย่าง: คุณสามารถดูการใช้งานhog()ดังนี้:

from skimage.feature import hog

print hog.__doc__

ผลลัพธ์จะเป็น:

Extract Histogram of Oriented Gradients (HOG) for a given image.
Compute a Histogram of Oriented Gradients (HOG) by

    1. (optional) global image normalisation
    2. computing the gradient image in x and y
    3. computing gradient histograms
    4. normalising across blocks
    5. flattening into a feature vector

Parameters
----------
image : (M, N) ndarray
    Input image (greyscale).
orientations : int
    Number of orientation bins.
pixels_per_cell : 2 tuple (int, int)
    Size (in pixels) of a cell.
cells_per_block  : 2 tuple (int,int)
    Number of cells in each block.
visualise : bool, optional
    Also return an image of the HOG.
transform_sqrt : bool, optional
    Apply power law compression to normalise the image before
    processing. DO NOT use this if the image contains negative
    values. Also see `notes` section below.
feature_vector : bool, optional
    Return the data as a feature vector by calling .ravel() on the result
    just before returning.
normalise : bool, deprecated
    The parameter is deprecated. Use `transform_sqrt` for power law
    compression. `normalise` has been deprecated.

Returns
-------
newarr : ndarray
    HOG for the image as a 1D (flattened) array.
hog_image : ndarray (if visualise=True)
    A visualisation of the HOG image.

References
----------
* http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients

* Dalal, N and Triggs, B, Histograms of Oriented Gradients for
  Human Detection, IEEE Computer Society Conference on Computer
  Vision and Pattern Recognition 2005 San Diego, CA, USA

Notes
-----
Power law compression, also known as Gamma correction, is used to reduce
the effects of shadowing and illumination variations. The compression makes
the dark regions lighter. When the kwarg `transform_sqrt` is set to
``True``, the function computes the square root of each color channel
and then applies the hog algorithm to the image.

1
คำถามเกี่ยวกับนิยามฟังก์ชันไม่ใช่ฟังก์ชัน docstring
ctrl-alt-delor
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.