473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1162
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 automatic accessors to the elements of the dict?
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), set_Color(Color c)? from msdn: ...
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, x): .... print "ham * %s" % x .......
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): def do(s, m): print m @staticmethod def...
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 just make those variables public. After all,...
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 delegate int MyDelegate(string s); public event...
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 adding a web reference to the above file, I see...
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 to return a whole copy of the vector. What should...
7
by: Jaimi McEntire | last post by:
Given a property define like this: public decimal ControlAmount { get { return mControlAmount; } set { mControlAmount = value; } } I expected (under release mode) that when used as a...
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 methods doing unlinked initialisations: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.