Function Attribute funkiness...

Dan Parisien dan at eevolved.com
Sat Feb 3 20:13:03 EST 2001


I was reading up on function attributes. Incredible new feature... You can 
now add member functions to classes and class instances without having to 
inherit from them. Can be done at runtime!

Look what you can do with them


------picklesocket.py------
from cPickle import loads, dumps, UnpickleError

def unpickle(maxbytes):
        data = self.sock.recv(maxbytes)
        return dumps(data)

def pickle(anything):
        """anything can be a list, dictionary, tuple, string.. you name it
        """
        data = loads(anything)
        self.sock.send(data)

def patchSocket(instance):
        pickle.sock = unpickle.sock = instance
        instance.pickle = pickle
        instance.unpickle = unpickle
        

sock = socket(...)

patchSocket(sock) # adds new member methods : pickle and unpickle




More information about the Python-list mailing list