473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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_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.
Sep 1 '08 #1
0 761

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: srijit | last post by:
Hello All, I have been seeing this term "duck typing" for quite sometime now. It will be nice if one of us can give an example in Python demonstrating duck typing and/or link to some Python...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
2
by: Andrzej Kaczmarczyk | last post by:
Hi I am experiencing something weird. maybe you could help me. I have two ineditable classes from outsource libraries: DataColumn and GridColumn I have built a wrapper class around...
5
by: Michael Dyremo | last post by:
Hello. We have a web-application built in ASP.NET using WebForms and Remoting. When selling this application we always incorporate it into the customers existing web-site. Our latest customer...
1
by: insyte | last post by:
I am writing a class that subclasses datetime.datetime in order to add a few specialized methods. So far the __init__ looks like this: class myDateTime(datetime.datetime): def __init__(self,...
0
by: Paddy | last post by:
The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46 says: " duck-typing Pythonic programming style that determines an object's type by inspection of its method or...
0
by: Neville Dempsey | last post by:
What do I need to add to HTMLDecorator? A simpler example: import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object):
16
by: Joe Strout | last post by:
Let me preface this by saying that I think I "get" the concept of duck- typing. However, I still want to sprinkle my code with assertions that, for example, my parameters are what they're...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.