擅长:python、mysql、java
<p>这是有效的:</p>
<pre><code>def my_function():
"""Docstring for my function"""
#print the Docstring here.
print my_function.__doc__
my_function()
</code></pre>
<p>在Python2.7.1中</p>
<p>这也适用于:</p>
<pre><code>class MyClass(object):
def my_function(self):
"""Docstring for my function"""
#print the Docstring here, either way works.
print MyClass.my_function.__doc__
print self.my_function.__doc__
foo = MyClass()
foo.my_function()
</code></pre>
<p>然而,这不会单独起作用:</p>
<pre><code>class MyClass(object):
def my_function(self):
"""Docstring for my function"""
#print the Docstring here.
print my_function.__doc__
foo = MyClass()
foo.my_function()
</code></pre>
<p>名称错误:未定义全局名称“my_function”</p>