Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91845 > unrolled thread

Re: for...else

Started byLaura Creighton <lac@openend.se>
First post2015-06-02 16:29 +0200
Last post2015-06-02 20:16 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: for...else Laura Creighton <lac@openend.se> - 2015-06-02 16:29 +0200
    Re: for...else "ast" <nomail@invalid.com> - 2015-06-02 19:46 +0200
      Re: for...else Laura Creighton <lac@openend.se> - 2015-06-02 20:16 +0200

#91845 — Re: for...else

FromLaura Creighton <lac@openend.se>
Date2015-06-02 16:29 +0200
SubjectRe: for...else
Message-ID<mailman.64.1433255400.13271.python-list@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

[toc] | [next] | [standalone]


#91869

From"ast" <nomail@invalid.com>
Date2015-06-02 19:46 +0200
Message-ID<556dec13$0$3179$426a34cc@news.free.fr>
In reply to#91845
"Laura Creighton" <lac@openend.se> a écrit dans le message de 
news:mailman.64.1433255400.13271.python-list@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

[toc] | [prev] | [next] | [standalone]


#91879

FromLaura Creighton <lac@openend.se>
Date2015-06-02 20:16 +0200
Message-ID<mailman.74.1433268978.13271.python-list@python.org>
In reply to#91869
In a message of Tue, 02 Jun 2015 19:46:54 +0200, "ast" writes:
>
>
>"Laura Creighton" <lac@openend.se> a écrit dans le message de 
>news:mailman.64.1433255400.13271.python-list@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web