Hi!
How can I add a method to an object.
This code does not work:
class Foo:
def __init__(self):
self.counter=0
f=Foo()
def incr(self):
self.counter+=1
f.incr=incr
f.incr()
===> python extend.py
Traceback (most recent call last):
File "extend.py", line 12, in ?
f.incr()
TypeError: incr() takes exactly 1 argument (0 given) 8 1372
On Wed, 03 Dec 2003 10:44:12 +0100, "Thomas Guettler"
<gu*****@thomas-guettler.de> wrote: Hi!
How can I add a method to an object. This code does not work:
class Foo: def __init__(self): self.counter=0
f=Foo()
def incr(self): self.counter+=1
f.incr=incr
f.incr()
===> python extend.py Traceback (most recent call last): File "extend.py", line 12, in ? f.incr() TypeError: incr() takes exactly 1 argument (0 given)
You have to call it like
f.incr(f)
If you want to leave out the f as arg (call it like an instance
method) then import new help(new.instancemethod)
Help on class instancemethod in module __builtin__:
class instancemethod(object)
| instancemethod(function, instance, class)
|
| Create an instance method object.
|
....
And
new.instancemethod(incr, f, f.__class__)
Should do the trick.
With my best regards,
G. Rodrigues
Thomas Guettler wrote: How can I add a method to an object.
self is not passed automatically, if you call an instance attribute.
You can provide a default value instead: class Foo:
.... def __init__(self):
.... self.counter = 0
.... f = Foo() def incr(self=f):
.... self.counter += 1
.... f.incr = incr f.incr() f.counter
1
If you have more than one instance with the same incr() method, you can wrap
it into a lambda:
g = Foo() g.incr = lambda self=g: incr(self) g.incr() g.incr() g.counter
2
Peter
Is it possible to do the same thing for an attribut, instead of a method ?
i'd like to wrap an newAttribute to an oldAttribute one :
example:
i've got an instance "n" of an "xmlNode" class
i'd like to use "n.parentNode" instead of "n.parent" ...
"Gonçalo Rodrigues" <op*****@mail.telepac.pt> a écrit dans le message de
news: 72********************************@4ax.com... On Wed, 03 Dec 2003 10:44:12 +0100, "Thomas Guettler" <gu*****@thomas-guettler.de> wrote:
Hi!
How can I add a method to an object. This code does not work:
class Foo: def __init__(self): self.counter=0
f=Foo()
def incr(self): self.counter+=1
f.incr=incr
f.incr()
===> python extend.py Traceback (most recent call last): File "extend.py", line 12, in ? f.incr() TypeError: incr() takes exactly 1 argument (0 given)
You have to call it like
f.incr(f)
If you want to leave out the f as arg (call it like an instance method) then
import new help(new.instancemethod)
Help on class instancemethod in module __builtin__:
class instancemethod(object) | instancemethod(function, instance, class) | | Create an instance method object. | ...
And
new.instancemethod(incr, f, f.__class__)
Should do the trick.
With my best regards, G. Rodrigues
"marco" <ma********@ctrceal.caisse-epargne.fr> wrote in message
news:bq**********@s1.read.news.oleane.net... Is it possible to do the same thing for an attribut, instead of a method ?
i'd like to wrap an newAttribute to an oldAttribute one : example: i've got an instance "n" of an "xmlNode" class i'd like to use "n.parentNode" instead of "n.parent" ...
[snip]
# try this ...
n.parentNode = n.parent
HTH
Sean
> > Is it possible to do the same thing for an attribut, instead of a method
? i'd like to wrap an newAttribute to an oldAttribute one : example: i've got an instance "n" of an "xmlNode" class i'd like to use "n.parentNode" instead of "n.parent" ...
[snip]
# try this ... n.parentNode = n.parent
i'll try,
but ... it s not dynamic ....
if n.parent change .... n.parentNode will not change ... it must be
re-affected ... not ?
"marco" <ma********@ctrceal.caisse-epargne.fr> wrote in message
news:bq**********@s1.read.news.oleane.net... Is it possible to do the same thing for an attribut, instead of a
method ? i'd like to wrap an newAttribute to an oldAttribute one : example: i've got an instance "n" of an "xmlNode" class i'd like to use "n.parentNode" instead of "n.parent" ... [snip]
# try this ... n.parentNode = n.parent
i'll try, but ... it s not dynamic ....
if n.parent change .... n.parentNode will not change ... it must be re-affected ... not ?
You're right. Sorry about that. Maybe you could use a property?
n.__class__.parentNode = property(lambda self: self.parent, ... etc ... )
This way you can get/set/del n.parent using n.parentNode, and changes
to n.parent will be reflect in n.parentNode.
There's probably another way, but I can't think of it at the moment...
Hope that's a little more helpful than the last suggestion,
Sean
Sean Ross wrote: "marco" <ma********@ctrceal.caisse-epargne.fr> wrote in message news:bq**********@s1.read.news.oleane.net...
Is it possible to do the same thing for an attribut, instead of a
method
?
i'd like to wrap an newAttribute to an oldAttribute one : example: i've got an instance "n" of an "xmlNode" class i'd like to use "n.parentNode" instead of "n.parent" ...
[snip]
# try this ... n.parentNode = n.parent
i'll try, but ... it s not dynamic ....
if n.parent change .... n.parentNode will not change ... it must be re-affected ... not ?
You're right. Sorry about that. Maybe you could use a property?
n.__class__.parentNode = property(lambda self: self.parent, ... etc ... )
This way you can get/set/del n.parent using n.parentNode, and changes to n.parent will be reflect in n.parentNode.
There's probably another way, but I can't think of it at the moment...
Hope that's a little more helpful than the last suggestion, Sean
perfect ! it works like a charm
thanx a lot !
In article <72********************************@4ax.com>, Gonçalo Rodrigues wrote: If you want to leave out the f as arg (call it like an instance method) then
import new help(new.instancemethod)
Help on class instancemethod in module __builtin__:
class instancemethod(object) | instancemethod(function, instance, class) | | Create an instance method object. | ...
And
new.instancemethod(incr, f, f.__class__)
Should do the trick.
I tried to bring this up several weeks ago but nobody replied, so I'm
bringing it up again. I still see people recommending "new.instancemethod",
yet "help(new)" says that the "new" module is deprecated. The seemingly
identical "types.MethodType" ought to be its replacement, even though I
think "new.instancemethod" is more clear. If you stringify types.MethodType,
it says "<type 'instancemethod'>". The help for instancemethod, above, says
that instancemethod is in the __builtin__ module, but it is neither a
builtin nor available in the __builtin__ module. This is confusing. Can we
decide on a community standard for the appropriate way to create new
instance methods, and resolve the documentation discrepancies?
Thankya kindly,
Dave
--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r : This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: laredotornado |
last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to
have my Javascript function execute from the BODY's "onload" method,
but if there is already an onload method defined, I would...
|
by: Steven D'Aprano |
last post by:
Suppose I create a class with some methods:
py> class C:
.... def spam(self, x):
.... print "spam " * x
.... def ham(self, x):
.... print "ham * %s" % x
.......
|
by: Gabriele *darkbard* Farina |
last post by:
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...
|
by: DotNetJunky |
last post by:
I have built a control that runs an on-line help system. Depending on the
category you selected via dropdownlist, it goes out and gets the child
subcategories, and if there are any, adds a new...
|
by: Jim Heavey |
last post by:
Trying to figure out the technique which should be used
to add rows to a datagrid. I am thinking that I would
want an "Add" button on the footer, but I am not quite
sure how to do that. Is that...
|
by: Ankit Aneja |
last post by:
I have a strange situation and I have no idea how to solve this.
Its a Recruitment Search Page,in the Admin Page, for every button click
event the Admin Person has to create a checkbox on the users...
|
by: Trevor |
last post by:
Hey,
I am trying to do this tutorial on the microsoft site :
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dndotnet/html/usingadonet.asp
I can get everything to work up to...
|
by: marek.rocki |
last post by:
First of all, please don't flame me immediately. I did browse archives
and didn't see any solution to my problem.
Assume I want to add a method to an object at runtime. Yes, to an
object, not a...
|
by: silversurfer |
last post by:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:
std::vector<Entry> models;
struct...
|
by: jholg |
last post by:
Hi,
regarding automatically adding functionality to a class (basically taken
from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code:
#-----------------...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |