By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

How can I make a class that can be converted into an int?

P: n/a
What are the internal methods that I need to define on any class so that
this code can work?

c = C("three")

i = int(c) # i is 3

I can handle the part of mapping "three" to 3, but I don't know what
internal method is called when int(c) happens.

For string conversion, I just define the __str__ method. What's the
equivalent for int? For float, too, while I'm at it?

TIA

Matt

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Oct 2 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
__int__
__long__
__float__
Matthew Wilson wrote:
What are the internal methods that I need to define on any class so that
this code can work?

c = C("three")

i = int(c) # i is 3

I can handle the part of mapping "three" to 3, but I don't know what
internal method is called when int(c) happens.

For string conversion, I just define the __str__ method. What's the
equivalent for int? For float, too, while I'm at it?

TIA

Matt

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Oct 2 '06 #2

P: n/a
What are the internal methods that I need to define on any class so that
this code can work?

c = C("three")

i = int(c) # i is 3

I can handle the part of mapping "three" to 3, but I don't know what
internal method is called when int(c) happens.

For string conversion, I just define the __str__ method. What's the
equivalent for int? For float, too, while I'm at it?
Is it too unkind to say it's semi-obvious?
>>class Impersonator(object):
.... def __str__(self): return "I'm a string"
.... def __int__(self): return 42
.... def __float__(self): return 3.14159
....
>>c = Impersonator()
float(c)
3.1415899999999999
>>int(c)
42
>>str(c)
"I'm a string"

You say you can handle the conversion of "three" to 3, so I leave
that implementation of __int__(self) to you... :)

-tkc

Oct 2 '06 #3

P: n/a


Matthew Wilson wrote:
What are the internal methods that I need to define on any class so that
this code can work?

c = C("three")

i = int(c) # i is 3
From Python Reference Manual, section 3.4.7 Emulating numeric types:

__complex__( self)
__int__( self)
__long__( self)
__float__( self)
Called to implement the built-in functions complex(), int(),
long(), and float(). Should return a value of the appropriate type.

/MiO
Oct 2 '06 #4

P: n/a
Matthew Wilson wrote:
What are the internal methods that I need to define on any class so that
this code can work?

c = C("three")

i = int(c) # i is 3

I can handle the part of mapping "three" to 3, but I don't know what
internal method is called when int(c) happens.
>>class C(object):
.... def __int__(self): return 42
....
>>int(C())
42
For string conversion, I just define the __str__ method. What's the
equivalent for int? For float, too, while I'm at it?
http://docs.python.org/ref/numeric-types.html

Peter
Oct 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.