Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
...

You can see that lots of information is provided by the help function, although it is a little cryptic
for those new to Python. A nice site to visit when you need a little more help on modules than you get
from help is docs.python.org/3/py-modindex.html.


By the Way: Different help Endings
Sometimes using help in the Python interactive shell just puts you back at the shell
prompt, as when you use the command help('modules'). Other times, such as
when issuing the command help(calendar), you need to use special keys to
navigate and quit the help function. This is covered in Hour 3, “Setting Up a
Programming Environment.” Review the section “Learning About the Python
Interactive Shell” in that hour if you need a refresher.

For a quick list of functions, contained within a module, you can use the dir function. For example,
Listing 13.5 shows a list of the functions the calendar module provides.


LISTING 13.5 Using dir to List Functions Within a Module


Click here to view code image


>>> import calendar
>>> dir(calendar)
['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar',
'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar',
'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY',
'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__',
'__cached__', '__doc__', '__file__', '__name__', '__package__', '_colwidth',
'_locale', '_localized_day', '_localized_month', '_spacing', 'c', 'calendar',
'datetime', 'day_abbr', 'day_name', 'different_locale', 'error',
'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main',
'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange',
'prcal', 'prmonth', 'prweek', 'setfirstweekday', 'sys', 'timegm', 'week',
'weekday', 'weekheader']
>>>

Notice that one of the functions that calendar provides is prcal. You can get help on a particular
function, such as prcal, without digging through all the other module help information. You use the
syntax help(module.function), as shown in Listing 13.6.


LISTING 13.6 Using help to See How to Use a Function


Click here to view code image


>>> import calendar
>>> help(calendar.prcal)

Help on method pryear in module calendar:

pryear(self, theyear, w=0, l=0, c=6, m=3) method of calendar.TextCalendar instance
Free download pdf