简体   繁体   中英

Why isn't this docstring displaying?

Just getting started with "Dive Into Python". For some reason I can't get a docstring to display.

#!/usr/bin/env python

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters
    """
    # Returns string
    return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

if __name__ == "__main__":
    myParams = {"server":   "mpilgrim", \
                "database": "master",   \
                "uid":      "sa",       \
                "pwd":      "secret"    \
                }
    print buildConnectionString(myParams)

At the console:

>>> import odbchelper
>>> print odbchelper.buildConnectionString.__doc__
None
>>> 

This works fine:

>>> import sys
>>> sys.path.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"
>>> 

I tried a number of permuations, including """ and # comments. No joy. Where is the problem?

What you have should work; some possible reasons why it is not:

  • You are editing a different file
  • You haven't exited and restarted the interpreter after editing the correct file

Update:

If you use reload to avoid exiting and restarting the interpreter (or IDLE or whatever you are using) you need to be aware of a couple pitfalls:

  • reload is not recursive

In other words, if your odbchelper imports odbcstuff and you reload(odbchelper) , odbcstuff will not be reloaded.

  • reload does not update other existing objects

If you try to get around the non-recursive nature of reload with

import odbcstuff
reload(odbcstuff)

the odbcstuff that odbchelper sees is still the old one; you need one more step:

odbchelper.odbcstuff = odbcstuff     # update odbchelper with the reloaded odbcstuff

As you can see, that would be quite tedious for more than a couple dependent modules. So go ahead and use reload , but if things are still not working correctly, exit and restart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM