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

Discussion in 'Python' started by Matthew Wilson, Oct 2, 2006.

  1. 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/wilsonwiki/SasAndMakefiles
     
    Matthew Wilson, Oct 2, 2006
    #1
    1. Advertising

  2. Matthew Wilson

    faulkner Guest

    __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/wilsonwiki/SasAndMakefiles
     
    faulkner, Oct 2, 2006
    #2
    1. Advertising

  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
     
    Mikael Olofsson, Oct 2, 2006
    #3
  4. Matthew Wilson

    Peter Otten Guest

    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
     
    Peter Otten, Oct 2, 2006
    #4
  5. Matthew Wilson

    Tim Chase Guest

    > 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
     
    Tim Chase, Oct 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  3. Spendius
    Replies:
    3
    Views:
    982
    Babu Kalakrishnan
    Aug 26, 2006
  4. arun
    Replies:
    8
    Views:
    481
    Dave Thompson
    Jul 31, 2006
  5. Replies:
    3
    Views:
    439
    red floyd
    Apr 7, 2006
Loading...

Share This Page