469,917 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Any way of adding methods/accessors to built-in classes?

This is possible with pure Python classes. Just add the method as new
attribute of the class. However, that won't work for the builtins.

I know that this is somewhat dangerous, and also that I could subclass
the builtins, but not being able to do things like '[1,2,3]'.length
drives me a little nuts. Python is about the only computer language I
use, and I think it's certainly the best of the scripting languages, but
there are inconsistencies in the object model and some other things I
wish I could fix. If could could modify the builtins this way, I'd be
willing to take the risk.

Thanks,
Ken
Oct 25 '06 #1
3 1068
Kenneth McDonald:
not being able to do things like '[1,2,3]'.length
drives me a little nuts.
This is interesting, why?
(In a computer language too much purity is often bad. And isn't
[1,2,3].len better?)

I think you can't add methods to Python builtin classes, I think you
can do it with Ruby.

Bye,
bearophile

Oct 25 '06 #2

Kenneth McDonald wrote:
This is possible with pure Python classes. Just add the method as new
attribute of the class. However, that won't work for the builtins.

I know that this is somewhat dangerous, and also that I could subclass
the builtins, but not being able to do things like '[1,2,3]'.length
drives me a little nuts. Python is about the only computer language I
use, and I think it's certainly the best of the scripting languages, but
there are inconsistencies in the object model and some other things I
wish I could fix. If could could modify the builtins this way, I'd be
willing to take the risk.

Thanks,
Ken
Hi Ken,
The reason comes from code being read much more than it is written. It
makes it much easier to maintain code if some foundations i.e. the
builtins, don't change. That way everyone maintaining the code will
know that an int is an int, is an int, wherever it may be in the source
files of your program. The flip-side is that someone reading List in
athers code knows it isn't the built-in list type and so is on-guard
for any deviations/additions.

As for your irritation Ken, if you have to maintain code, as I do/have
done, then you remember how many times little inconsistencies have
tripped you up in the past, and offset those gripes against things like
this.

- cheers, Paddy.

Oct 25 '06 #3
On 2006-10-25, be************@lycos.com <be************@lycos.comwrote:
Kenneth McDonald:
>not being able to do things like '[1,2,3]'.length
drives me a little nuts.

This is interesting, why?
(In a computer language too much purity is often bad. And isn't
[1,2,3].len better?)

I think you can't add methods to Python builtin classes, I
think you can do it with Ruby.
You can create derived versions of builtins with new methods
(which affords the benefits of modifying builtins), but
you can't change the type of a Python literal. So the above would
be

MyList([1,2,3]).len

--
Neil Cerutti
Oct 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Christopher J. Bottaro | last post: by
11 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by Gabriele *darkbard* Farina | last post: by
6 posts views Thread by Jason Shohet | last post: by
1 post views Thread by Brad Williams | last post: by
1 post views Thread by Jeff Dillon | last post: by
2 posts views Thread by Alan Ning | last post: by
7 posts views Thread by Jaimi McEntire | last post: by
6 posts views Thread by =?ISO-8859-1?Q?Une_B=E9vue?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.