Add methods to int?

  • Thread starter Reinhold Birkenfeld
  • Start date
P

Peter Otten

Reinhold said:
I found this recipe at the Python cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/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:
False

Therefore you have to subclass them to gain the ability to add methods:
.... def __new__(cls, n):
.... return int.__new__(cls, n)
....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:

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
 
R

Reinhold Birkenfeld

Peter said:
int and str have no __dict__, neither in the class nor instance, and hence
no place to store your additions:

False

So why does "print int.__dict__" produce a value?
Therefore you have to subclass them to gain the ability to add methods:

... def __new__(cls, n):
... return int.__new__(cls, n)
...
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:

4

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

Reinhold
 
P

Peter Otten

Reinhold said:
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
 
R

Reinhold Birkenfeld

Peter said:
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
 
P

Peter Otten

Reinhold said:
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.
 
M

Michael Hudson

Peter Otten said:
PS: If you know whether read-only dictionaries are a technical or political
decision, don't hold back to tell us.

technical.

Cheers,
mwh
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top