improve this newbie code/nested functions in Python?

Esmail ebonak at hotmail.com
Sat Mar 21 08:41:03 EDT 2009


Paul Hankin wrote:
>
> 
> I would decouple the speaker and the listener from the client, and
> make the client interface more abstract. Simple and descriptive
> interfaces   can make code dramatically easier to understand.
> 
> class ClientListener(Thread):
>   def __init__(self, client, ...):
>     ...
> 
>   def run(self):
>     while True:
>        m = self.client.receive_message()
>        print m
> 
> class ClientSpeaker(Thread):
>   def __init__(self, client):
>     client.connect()
>   ...
>   def run(self):
>     while True:
>       m = raw_input()
>       if m == 'bye':
>         self.client.disconnect()
>         ...
>       else:
>         self.client.send_message(m)
> 
> The 'connect' method can print the client name, the 'disconnect' can
> say 'bye' and set _KeepGoing. receive_message can extract the
> interesting bit from the socket read. Hope you get the idea.
> 
> This way, you don't use the inner workings of client in the Listener
> and Speaker classes, and don't violate the 'law of demeter'. If
> nothing else, it makes the speaker and listener much easier to read.
> If you later want to test these classes, you'll find it a ton easier,
> since you can mock the client class.. but perhaps that's a little too
> advanced.

Hi Paul,

Yes that was very helpful -- I'm glad I found this group :-)

Best,
Esmail

> --
> Paul Hankin
> --
> http://mail.python.org/mailman/listinfo/python-list
> 






More information about the Python-list mailing list