473,395 Members | 1,441 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,395 software developers and data experts.

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

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
4 909
__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
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


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

Similar topics

5
by: Peter Meier | last post by:
Hello everybody, Stroustrup says he prefer's to declare operators, which do not do anything on the class itself global. Does anybody know the reason for that? Any advantages/disadvantages? ...
3
by: Kaz Kylheku | last post by:
Given some class C with array T x, is it possible to get a pointer-to-data-member to one of the elements? &C::x gives us a pointer-to-member-array: T (C::*). But I just want to get a T C::*...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
5
by: D Miller | last post by:
Hello, I want to use a class I have created, when I do I receive an error message that says the class can not be converted to string and another message saying that the class can not be...
0
by: Peter M | last post by:
I found this encryption class in the newsgroups and wanted to update this code so that the encryption is 512, not 256.... What has to be done to do this? Imports System Imports...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
10
by: Joseph Turian | last post by:
Is it possible to allow a class to be converted to bool? For example, if I have: class foo { private: unsigned i; }; and I want to evaluate: if (foo)
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.