enhancing/wrapping an existing instance of a duck
Neville Dempsey
nevillednz at gmail.com
Mon Sep 1 00:00:17 EDT 2008
Basically I have an existing (maybe a rather large and complicated
(existing) instance) that
I want to add new member to.
Cheers
N
Hacks/attempts follow:
from math import sqrt
############ try2 ############
duck_obj = [ i*i for i in range(25) ] # OR a large sparse matrix
# I "want" to an a useful property, eg length, and retain the ducks
existing properties.
# I COULD try...
setattr(duck_obj,"length",lambda: sqrt(sum(*duck_obj)))
print duck_obj.length() # returns 70
duck_obj[0]=70+71
print duck_obj.length() # returns 71
############ try2 ############
# **BUT** I'd rather encapsulate a the original instance somehow.
# I presume that I could define a class to do this somehow?
duck_obj = [ i*i for i in range(25) ] # OR a LargeSparseMatrix()
dec = Vec(duck_obj) ???
print dec.length() # returns 70
duck_obj[0]=70+71 # original "large and complicated duck instance"
print dec.length() # returns 71
Any hints on how I need to define Vec so that any kind of duck_obj can
be decorated/wrapped/encapsulated.
More information about the Python-list
mailing list