Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > enhancing/wrapping an existing instance of a duck

Reply
Thread Tools

enhancing/wrapping an existing instance of a duck

 
 
Neville Dempsey
Guest
Posts: n/a
 
      09-01-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.
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-01-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
duck duck go peter NZ Computing 1 01-09-2011 05:19 AM
Existing Dll - using Functions from an existing dll Tristin.Colby@gmail.com Ruby 0 02-05-2008 07:38 PM
Can you create an instance of a subclass with an existing instance of the base class? Sandra-24 Python 18 04-29-2006 04:01 PM
Why no existing Java type to existing XML schema binding support? nrm Java 3 04-10-2006 04:52 PM
[newbie] The Duck Problem, or accessing one instance var from another Vladimir Agafonkin Ruby 6 01-24-2006 06:06 PM



Advertisments