for...else

Laura Creighton lac at openend.se
Tue Jun 2 14:16:07 EDT 2015


In a message of Tue, 02 Jun 2015 19:46:54 +0200, "ast" writes:
>
>
>"Laura Creighton" <lac at openend.se> a écrit dans le message de 
>news:mailman.64.1433255400.13271.python-list at python.org...
>> You may be looking for dictionary dispatching.
>>
>> You can translate the key into a callable.
>>
>> def do_ping(self, arg):
>>    return 'Ping, {0}!'.format(arg)
>>
>> def do_pong(self, arg):
>>    return 'Pong, {0}!'.format(arg)
>>
>> dispatch = {
>>    'ping': do_ping,
>>    'pong': do_pong
>> }
>>
>> and then at some point do
>>
>>    dispatch[command](arg)
>>
>> It is useful to know that tuples make perfectly good dictionary keys,
>> in case you need to store more state somewhere.
>>
>> Laura
>
>hello
>
>Why "self" is as parameter of functions do_ping and do_pong ?
>
>thx

yanked out of code I had lying around, without proper thought.
Sorry about that.

Laura




More information about the Python-list mailing list