Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Preferred idion for adding instance methods

Reply
Thread Tools

Preferred idion for adding instance methods

 
 
Dave Benjamin
Guest
Posts: n/a
 
      10-14-2003
I just noticed that the "new" module is deprecated in Python 2.3. Since the
old way of adding a method to a particular instance (not its class) was to
use new.instancemethod, I am guessing that we are now supposed to use
types.MethodType. Is this the preferred idiom for adding an instance method?

import new
import types

class Test(object):
def __init__(self, x):
self.x = x

t = Test(42)

def print_x(self):
print self.x

# The old way.
#t.print_x = new.instancemethod(print_x, t, Test)

# The new way?
t.print_x = types.MethodType(print_x, t, Test)

t.print_x()

Thanks,
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 :
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
difference between class methods and instance methods John M. Gabriele Python 18 02-18-2005 05:17 PM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM
Preferred Instance Variable Access Method Bill Atkins Ruby 3 04-08-2004 03:22 AM



Advertisments