Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Any way of adding methods/accessors to built-in classes?

Reply
Thread Tools

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

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      10-25-2006
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
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      10-25-2006
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

 
Reply With Quote
 
 
 
 
Paddy
Guest
Posts: n/a
 
      10-25-2006

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.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-25-2006
On 2006-10-25, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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
 
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
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
[OT] Any way to see if a website is up in an automated way? Rhino Java 5 09-05-2006 12:42 PM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM
Adding Pub any other way? B.W. HTML 3 10-03-2003 06:50 AM



Advertisments