[Tutor] General Feedback, Script Structure
Kent Johnson
kent37 at tds.net
Sun Feb 15 20:25:59 CET 2009
On Sun, Feb 15, 2009 at 1:01 PM, Damon Timm <damontimm at gmail.com> wrote:
> On Sun, Feb 15, 2009 at 12:20 PM, Kent Johnson <kent37 at tds.net> wrote:
>> - put the main code in a main() function rather than splitting it
>> across the file.
>
> That's a good idea - I will do that. Is it proper to create a def
> main() or just under: if __name__ == "__main__"
Either one works; I usually don't put more than a few lines of code in
the if block so I would make a main() function.
>> then you can build the message as
>> body = status.get(event, nomatch) % vars()
>> message = header + body + footer
>
>
> That last part I am not so clear on ... how does: body =
> status.get(event, nomatch) % vars() work ? Does it say, first look
> for "event" as a key and then, if it doesn't find a match with event,,
> use the "nomatch" key ?
Exactly. dict.get() does a key lookup with a default for missing keys,
then the result is used as a string format. See
http://docs.python.org/library/stdtypes.html#dict.get
BTW I strongly recommend becoming familiar with the Built-in Functions
and Built-in Types sections of the standard library docs:
http://docs.python.org/library/
Kent
More information about the Tutor
mailing list