1

I am a newbie to python. Just try to get the comments defined in function using doc but getting the error.

Here's my code:

def nam(i):
    """passing the name to
    a function"""
    print('hello  '+i+' good mornin')

And here's the error:

 Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(nam._doc_)
AttributeError: 'function' object has no attribute '_doc_'
0

1 Answer 1

2

Replace _doc_ (single underline on each side) with __doc__ (double underlines)

To illustrate, let's define your function and display the documentation:

>>> def nam(i):
...     """passing the name to
...     a function"""
...     print('hello  '+i+' good mornin')
... 
>>> nam.__doc__
'passing the name to\n    a function'

The method above works. The method below, however, fails:

>>> nam._doc_
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '_doc_'

The reason is that __doc__ needs two underline characters on each side, not just one.

Documentation

From the python online documentation:

docstring
A string literal which appears as the first expression in a class, function or module. While ignored when the suite is executed, it is recognized by the compiler and put into the __doc__ attribute of the enclosing class, function or module. Since it is available via introspection, it is the canonical place for documentation of the object.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.