469,358 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Add methods to int?

Hello,

I found this recipe at the Python cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81732

Saucily I tried to apply this to int or str, with this result:

TypeError: object does not support item assignment

Any way to go round that?

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #1
7 2290
Reinhold Birkenfeld wrote:
I found this recipe at the Python cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81732

Saucily I tried to apply this to int or str, with this result:

TypeError: object does not support item assignment

Any way to go round that?


int and str have no __dict__, neither in the class nor instance, and hence
no place to store your additions:
"__dict__" in dir(4) False "__dict__" in dir(4 .__class__) False

Therefore you have to subclass them to gain the ability to add methods:
class Int(int): .... def __new__(cls, n):
.... return int.__new__(cls, n)
.... x = Int(2)
x.twice() Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Int' object has no attribute 'twice'

Now let's grow our Int class a twice() method:
def twice(self): return 2*self .... Int.twice = twice
x.twice() 4


I don't know for sure, but I suppose the tricks used in the recipe are no
longer necessary in current Python. Anyway, if you want to tack your custom
methods on the builtin integer and string classes, you're out of luck.

Peter

Jul 18 '05 #2
Peter Otten wrote:
int and str have no __dict__, neither in the class nor instance, and hence
no place to store your additions:
"__dict__" in dir(4) False "__dict__" in dir(4 .__class__) False
So why does "print int.__dict__" produce a value?
Therefore you have to subclass them to gain the ability to add methods:
class Int(int): ... def __new__(cls, n):
... return int.__new__(cls, n)
... x = Int(2)
x.twice() Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Int' object has no attribute 'twice'

Now let's grow our Int class a twice() method:
def twice(self): return 2*self ... Int.twice = twice
x.twice()

4


That's clear, yes. But the problem with this solution is that I must
convert all literals to Int before using them.

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #3
Reinhold Birkenfeld wrote:
So why does "print int.__dict__" produce a value?


Because I was wrong, I suppose. Anyway, there is code in descrobject.c that
wraps a dictionary/sequence type to ensure that it is readonly.
As to why it doesn't show up in dir() and why it is read-only, I have no
idea.

Peter

Jul 18 '05 #4
Peter Otten wrote:
Reinhold Birkenfeld wrote:
So why does "print int.__dict__" produce a value?


Because I was wrong, I suppose. Anyway, there is code in descrobject.c that
wraps a dictionary/sequence type to ensure that it is readonly.
As to why it doesn't show up in dir() and why it is read-only, I have no
idea.


So would that be a point where to make improvements?

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #5
Reinhold Birkenfeld wrote:
So would that be a point where to make improvements?


I'm not sure what you mean. Do you think it would be an improvement if int
and str could be customized? I doubt that. I can easily think of mutually
exclusive extensions that would prevent modules from different authors from
working together. If you have a compelling addition, why not suggest it for
inclusion in the next version of Python?

Another option is to adopt quixote's technique of turning str into htmltext.

Peter

PS: If you know whether read-only dictionaries are a technical or political
decision, don't hold back to tell us.
Jul 18 '05 #6
Peter Otten <__*******@web.de> writes:
PS: If you know whether read-only dictionaries are a technical or political
decision, don't hold back to tell us.


technical.

Cheers,
mwh

--
<faassen> I'm not a PSU agent.
-- from Twisted.Quotes
Jul 18 '05 #7
Reinhold Birkenfeld <re************************@wolke7.net> writes:
Peter Otten wrote:
int and str have no __dict__, neither in the class nor instance, and hence
no place to store your additions:
> "__dict__" in dir(4)

False
> "__dict__" in dir(4 .__class__)

False


So why does "print int.__dict__" produce a value?

"__dict__" in dir(4 .__class__.__class__)

True

:-)

Cheers,
mwh

--
If Unicode is a horde of zombies with flaming dung sticks,
the hideous intricacies of JIS, Chinese Big-5, Chinese
Traditional, KOI-8, et cetera are at least an army of ogres
with salt and flensing knives. -- Eric S. Raymond, python-dev
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
4 posts views Thread by Neil Zanella | last post: by
51 posts views Thread by Noam Raphael | last post: by
18 posts views Thread by John M. Gabriele | last post: by
32 posts views Thread by Adrian Herscu | last post: by
3 posts views Thread by rickeringill | last post: by
26 posts views Thread by Cliff Williams | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.