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. -- http://mail.python.org/mailman/listinfo/python-list
Re: enhancing/wrapping an existing instance of a duck
What do I need to add to HTMLDecorator? A simpler example: import cgi class ClassX(object): pass # ... with own __repr__ class ClassY(object): pass # ... with own __repr__ inst_x=ClassX() inst_y=ClassY() inst_z=[ i*i for i in range(25) ] inst_b=True class HTMLDecorator(object): def html(self): # an "enhanced" version of __repr__ return cgi.escape(self.__repr__()).join(("","")) print HTMLDecorator(inst_x).html() print HTMLDecorator(inst_y).html() wrapped_z = HTMLDecorator(inst_z) inst_z[0] += 70 wrapped_z[0] += 71 print wrapped_z.html() print HTMLDecorator(inst_b).html() Output: Traceback (most recent call last): File "html.py", line 21, in print HTMLDecorator(inst_x).html() TypeError: default __new__ takes no parameters Can I simply decorate an existing instance? Cheers N -- http://mail.python.org/mailman/listinfo/python-list
Re: if the else short form
On Oct 11, 11:46 am, Lawrence D'Oliveiro wrote: > Nowadays we take it for granted that the core language should be a strong > and compact basis to build on, rather than providing lots of built-in > features, and all the rest should come from run-time libraries. Fast forward to 1972... In 1972 British Military implemented USE, KEEP and HERE for ALGOL 68RS and then in 1978... Check out NEST and EGG in "Formal Definition of Modules and Separate Compilation". http://www.google.com.au/search?q=%22A-Modules-and-Separate-Compilation-facility-for-ALGOL-68%22 9.4.1.d module symbol{49a} MODULE access symbol{36b} ACCESS def symbol{49c} DEF fed symbol{49c} FED public symbol{36d,41e} PUB postlude symbol{49f} POSTLUDE {{Moreover, two more new symbols are yet to be invented for use in separate compilation:}} formal nest symbol{56b} NEST egg symbol{A6a,c}EGG These were defined to avoid things like C's problem with "#include" For example a typical 5 line program can require the compilation of a 163kb file, 5 linesor original source becomes almost 5 thousand lines of pre-processed source e.g.: $ cat hello_world.c #include #include int main(int c, char *argv, char *argp){ printf("Hello, world!\n"); } $ gcc -C -E hello_world.c -o hello_world.txt $ wc hello_world.txt 4554 24360 163915 hello_world.txt The idea would be to compile the text file one and generate one compiled file contains the required symbols... bingo, there you have it. Compilations are potentially 1000x faster? Hence the NEST and EGG idea. The Soviet GOST standard details the standardisation of NEST and EGG (Page 271): http://vak.ru/lib/exe/fetch.php/book/gost/pdf/gost-27975-88.pdf (altogether there are also: MODULE, ACCESS, DEF, FED, PUB, POSTLUDE, NEST & EGG) (BTW: The Soviet standard also details the use of ON, EXCEPTION and RAISE, on page 269) -- http://mail.python.org/mailman/listinfo/python-list