martin's
martin's

Reputation: 3955

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?

Upvotes: 0

Views: 370

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69081

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.

Upvotes: 2

Related Questions