472,337 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

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 1120
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christopher J. Bottaro | last post by:
If I have the following class: class MyClass: def __init__(self): m_dict = {} m_dict = 1 m_dict = 2 m_dict = 3 Is there anyway to generate...
2
by: cody | last post by:
Does the clr allow more than one set and one get method for a property? Is it possible to use overloading for example set_Color(int c),...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self,...
3
by: Gabriele *darkbard* Farina | last post by:
Hi, there is a way to add methods to an object dynamically? I need to do something like this. I remember python allowed this ... class A(object):...
6
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not...
1
by: Brad Williams | last post by:
When I try to define accessors for this event, the event invocation line stops compiling. What's wrong? public class TestClass { public...
1
by: Jeff Dillon | last post by:
I'm using the webservice I've created: http://www.emergencyreporting.com/ERSWebService/ERSDispatch.asmx When building a client VB.NET app, and...
2
by: Alan Ning | last post by:
I have a class that contains a stl vector as a private member. I would like to write an accessor method for this stl vector. However, I don't want...
7
by: Jaimi McEntire | last post by:
Given a property define like this: public decimal ControlAmount { get { return mControlAmount; } set { mControlAmount = value; } } I...
6
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
i'd like to intercept the window.onload event in order to distribute it, as needed, to several methods. example : suppose i have several...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.