enhancing/wrapping an existing instance of a duck

Discussion in 'Python' started by Neville Dempsey, Sep 1, 2008.

  1. 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.
     
    Neville Dempsey, Sep 1, 2008
    #1
    1. Advertising

  2. Neville Dempsey a écrit :
    > Basically I have an existing (maybe a rather large and complicated
    > (existing) instance) that
    > I want to add new member to.


    I suppose you mean "attributes" ?

    > 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)))


    Won't work on a list.

    > print duck_obj.length() # returns 70
    > duck_obj[0]=70+71
    > print duck_obj.length() # returns 71


    You obviously didn't try the above code.

    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    AttributeError: 'list' object has no attribute 'length'



    Also and FWIW, in Python, the "sizeable" protocol is implemented using a
    __len__ method, that will get called by the generic len(sizeable) function.

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


    Depends on the type of duck_obj. In the above case, I'd inherit from
    list and override __len__ :

    >>> import math
    >>> class Vec(list):

    .... def __len__(self):
    .... return math.sqrt(sum(self))
    ....
    >>> Vec(range(10))

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> len(Vec(range(10)))

    6
     
    Bruno Desthuilliers, Sep 1, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitriy Zakharov

    Enhancing ASP.NET Framework

    Dmitriy Zakharov, Sep 3, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    409
    Patrice
    Sep 6, 2004
  2. Sandra-24
    Replies:
    18
    Views:
    1,921
    Steve Holden
    Apr 29, 2006
  3. Replies:
    1
    Views:
    272
    Gabriel Genellina
    Nov 15, 2006
  4. Vladimir Agafonkin
    Replies:
    6
    Views:
    116
    Vladimir Agafonkin
    Jan 24, 2006
  5. Chicken McNuggets

    Wrapping existing UNIX commands in C

    Chicken McNuggets, Jul 29, 2012, in forum: C Programming
    Replies:
    87
    Views:
    1,815
    Kaz Kylheku
    Sep 10, 2012
Loading...

Share This Page