Re: Adding methods to an object instance

2009-11-13 Thread Bruno Desthuilliers
lallous a écrit : Hello class __object(object): the convention for reusing reserved words as identifiers is to *suffix* them with a single underscore, ie: class object_(object): # def __getitem__(self, idx): return getattr(self, idx) class __dobject(object): pass x = __ob

Re: Adding methods to an object

2005-10-13 Thread Steven D'Aprano
On Thu, 13 Oct 2005 06:06:20 -0700, Gabriele *darkbard* Farina wrote: > Hi, there is a way to add methods to an object dynamically? Yes. There are three different sorts of methods, and three ways of adding them. py> class Parrot: ... def __init__(self): ... pass py> # Parrot is the

Re: Adding methods to an object

2005-10-13 Thread rcramsdell
gabriele, This works (A, Test, and o as defined by you): >>> a=A() >>> o.do(a, 10) 10 Your problem is that do() really has two parameters, an A instance and whatever you want to print. Why not do this: >>> def newdo(m): ... print m ... >>> newdo(10) 10 >>> o=Test() >>> o.newdo = newdo >>>

Re: Adding methods to an object

2005-10-13 Thread bearophileHUGS
This isn't code of mine, it's probably from the cookbook, maybe with little changes: | def addMethod(object, method, name=None): | if name is None: name = method.func_name | class newclass(object.__class__): | pass | setattr(newclass, name, method) | object.__class__ = newc