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
Hello
class __object(object):
def __getitem__(self, idx):
return getattr(self, idx)
class __dobject(object): pass
x = __object()
setattr(x, "0", "hello")
print x["0"]
y = __dobject(a=1,b=2)
setattr(y, "0", "world")
#print y["0"]
How can I, given an object of instance "__dobject", a
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
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
>>>
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
Hi, there is a way to add methods to an object dynamically? I need to
do something like this. I remember python allowed this ...
class A(object):
def do(s, m):
print m
@staticmethod
def init(obj):
obj.do = A.do
class Test(object):
pass
o = Test()
A.init(o)
o.do(10)
Now it gives me an