Adding new methods to an instance with docstrings

Jim Meier jim at
Wed Aug 14 17:32:05 CEST 2002

On Tue, 13 Aug 2002 20:16:26 -0600, Brandon Beck wrote:

> Now to my problem.  I have a bunch of trivial functions I'd like to be 
> able to have as part of my scanner, however there are too many of them 
> to manually maintain as part of the scanner class.
> def add_token(type, attr):
> 	self.rv.append(Token(type=type, attr=attr))
> def add_method(obj, name, attr, doc):
> 	import new
> 	method = new.instancemethod(lambda attr: add_token('ID', attr), None, obj)
> 	method.__doc__ = doc
> 	setattr(obj, name, method)

What's wrong with putting these in a base class and having your scanner
inherit them?


More information about the Python-list mailing list