Home > Dive Into Python > Getting To Know Python > Everything is an object | << >> |
diveintopython.org | |
Python for experienced programmers | |
In case you missed it, I just said that Python functions have attributes, and that those attributes are available at runtime.
A function, like everything else in Python, is an object.
Example 1.5. Accessing the buildConnectionString function's doc string
>>> import odbchelper>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> print odbchelper.buildConnectionString(params)
server=mpilgrim;uid=sa;database=master;pwd=secret >>> print odbchelper.buildConnectionString.__doc__
Build a connection string from a dictionary Returns string.
Everything in Python is an object, so everything can have attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function's source code.
This is so important that I'm going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects, as we'll see shortly.
« Documenting functions | Indenting code » |