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

K

Kenneth McDonald

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
 
B

bearophileHUGS

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
 
P

Paddy

Kenneth said:
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.
 
N

Neil Cerutti

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top