enhancing/wrapping an existing instance of a duck

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neville Dempsey

    enhancing/wrapping an existing instance of a duck

    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_ob j,"length",lamb da: 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 LargeSparseMatr ix()

    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.
Working...