जावास्क्रिप्ट में, कोई फ़ंक्शन की परिभाषा को प्रिंट कर सकता है। क्या पायथन में इसे पूरा करने का कोई तरीका है?
(बस इंटरेक्टिव मोड में चारों ओर खेल रहा है, और मैं खुले बिना एक मॉड्यूल पढ़ना चाहता था ()। मैं बस उत्सुक था)।
जावास्क्रिप्ट में, कोई फ़ंक्शन की परिभाषा को प्रिंट कर सकता है। क्या पायथन में इसे पूरा करने का कोई तरीका है?
(बस इंटरेक्टिव मोड में चारों ओर खेल रहा है, और मैं खुले बिना एक मॉड्यूल पढ़ना चाहता था ()। मैं बस उत्सुक था)।
जवाबों:
यदि आप फ़ंक्शन आयात कर रहे हैं, तो आप उपयोग कर सकते हैं 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)
यह इंटरेक्टिव प्रॉम्प्ट में काम करेगा , लेकिन जाहिरा तौर पर केवल उन वस्तुओं पर आयात किया जाता है (इंटरेक्टिव प्रॉम्प्ट में परिभाषित ऑब्जेक्ट नहीं)। और निश्चित रूप से यह केवल तभी काम करेगा जब पायथन सोर्स कोड पा सकता है (इसलिए बिल्ट-इन ऑब्जेक्ट्स, सी लिबास, .pyc फाइलें आदि)।
यदि आप iPython का उपयोग कर रहे हैं , तो आप function_name?सहायता प्राप्त करने के लिए उपयोग कर सकते हैं, और function_name??स्रोत को प्रिंट कर सकते हैं, यदि यह कर सकता है।
हालांकि मैं आम तौर पर सहमत हूँ कि 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
>>>
आप __doc__ कीवर्ड का उपयोग कर सकते हैं:
#print the class description
print string.__doc__
#print function description
print open.__doc__
__doc__ वास्तव में रिटर्न क्या है । न कुछ ज्यादा, न कुछ कम।
आप __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.