By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

The joys of Python

P: n/a
Hello all,

I have been using Python for almost a year now and by now I do 95% of my
programming in it except some DSP stuff in C++. I learnt Python's basics
in one week, yet the language still amazes me every day. Elegant
solutions just keep cropping up everywhere, it must be one of these
brain-impedance matches I suppose :-)

Yesterday I came up with this:

class Channel:
## Acts as a function call wrapper, a call to one instance
## calls all other wrapped functions on the (broadcast) channel
def __init__(self, func, other=None):
self.all = (other and other.all) or []
self.func = func

def __call__(self, *args):
for item in self.all:

## join other channel ("tune in")
def join(self, other):
self.all = other.all
Now I can easily create:

from UserList import UserList

class SyncList(UserList):
def __init__(self):
self.append = Channel(self.append)
self.extend = Channel(self.extend)

def __repr__(self):
return repr(

## synchronise to other list
def join(self, other):
And this allows for automatic peer-to-peer list synchronisation:
l1 = SyncList()
l2 = SyncList()
l1.extend((2, 3, 4))
l1 [1, 2, 3, 4] l2 [1, 2, 3, 4] l1 is l2

qed :)

Of course, all mutating methods need to be wrapped. The "classic"
approach would have been to have one conrete broadcaster and do

def append(self, items):
for child in self.children:

def extend(self, items):
for child in self.children:

... yawn!
Bottom line is, I like Python a lot! It is certainly the bright side of


__________________________________________________ ______________________
e-mail: |alphir at \/\/

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.