Comparing variable types

Discussion in 'Python' started by Kill Bill, Oct 26, 2003.

  1. Kill Bill

    Kill Bill Guest

    type(i) == "<type 'float'>"
    this always returns false. How come?
    type(i)returns <type 'float'> if i is a float so why isn't == working?
     
    Kill Bill, Oct 26, 2003
    #1
    1. Advertising

  2. Kill Bill

    KefX Guest

    >type(i) == "<type 'float'>"
    >this always returns false. How come?
    >type(i)returns <type 'float'> if i is a float so why isn't == working?


    Use isinstance(), like this:
    isinstance(i, float)
    This will return True if i is a float, False if not.

    - Kef
     
    KefX, Oct 26, 2003
    #2
    1. Advertising

  3. On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:
    > type(i) == "<type 'float'>"
    > this always returns false. How come?
    > type(i)returns <type 'float'> if i is a float so why isn't == working?


    Because "<type 'float'>" is a string :)

    You want
    import types
    type(i) == types.FloatType

    or
    type(i) == type(1.0)

    or in 2.2 and later you can simply do
    type(i) == float

    -Andrew.
     
    Andrew Bennetts, Oct 26, 2003
    #3
  4. "Kill Bill" <> wrote in message
    news:bnfbuj$10f3di$-berlin.de...
    > type(i) == "<type 'float'>"


    This compares the current type of i to type("string"), which is <type
    'str'>.

    You can do:
    >>> i = 1.3
    >>> type(i) == "<type 'float'>"


    False
    >>> str(type(i)) == "<type 'float'>"

    True


    > this always returns false. How come?
    > type(i)returns <type 'float'> if i is a float so why isn't == working?
    >



    What you probably want is:

    >>> import types
    >>> type(i) == types.FloatType

    True
    >>>


    Emile van Sebille
     
    Emile van Sebille, Oct 26, 2003
    #4
  5. Kill Bill

    Kill Bill Guest

    Can you tell me where you found that method in the docs? I'm having trouble
    navigating them, the java docs are so much easier to look at.

    "KefX" <> wrote in message
    news:...
    > >type(i) == "<type 'float'>"
    > >this always returns false. How come?
    > >type(i)returns <type 'float'> if i is a float so why isn't == working?

    >
    > Use isinstance(), like this:
    > isinstance(i, float)
    > This will return True if i is a float, False if not.
    >
    > - Kef
     
    Kill Bill, Oct 26, 2003
    #5
  6. Kill Bill

    KefX Guest

    >What you probably want is:
    >
    >>>> import types
    >>>> type(i) == types.FloatType

    >True
    >>>>


    Using isinstance() as I described earlier is probably better because I think it
    works better with the idea of unifying types and classes.

    - Kef
     
    KefX, Oct 26, 2003
    #6
  7. Kill Bill

    KefX Guest

    >Can you tell me where you found that method in the docs?

    It's a builtin, meaning it's in module __builtin__. You'll find them in the
    library reference near the top under "built-in functions" or some such.

    - Kef
     
    KefX, Oct 26, 2003
    #7
  8. On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:
    > Can you tell me where you found that method in the docs? I'm having trouble
    > navigating them, the java docs are so much easier to look at.


    Library Reference, section 2.1: Built-in Functions
    http://python.org/doc/current/lib/built-in-funcs.html

    -Andrew.
     
    Andrew Bennetts, Oct 26, 2003
    #8
  9. Kill Bill

    Kill Bill Guest

    Where can I find all the methods for Dictionaries? They list some here, but
    is that all of them?
    http://www.python.org/doc/2.3.2/tut/node7.html#SECTION007140000000000000000
    I think it is, but I don't like how its written. I want it to be written
    you know by method, constuctors, like in the Java API. Anyone know what I'm
    talking about? Its so quick to skim through the API to see which method you
    are looking for. Not the case here.

    "Andrew Bennetts" <> wrote in message
    news:...
    > On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:
    > > Can you tell me where you found that method in the docs? I'm having

    trouble
    > > navigating them, the java docs are so much easier to look at.

    >
    > Library Reference, section 2.1: Built-in Functions
    > http://python.org/doc/current/lib/built-in-funcs.html
    >
    > -Andrew.
    >
    >
     
    Kill Bill, Oct 26, 2003
    #9
  10. On Sun, Oct 26, 2003 at 12:44:20AM -0400, Kill Bill wrote:
    > Where can I find all the methods for Dictionaries? They list some here, but
    > is that all of them?
    > http://www.python.org/doc/2.3.2/tut/node7.html#SECTION007140000000000000000
    > I think it is, but I don't like how its written. I want it to be written
    > you know by method, constuctors, like in the Java API. Anyone know what I'm
    > talking about? Its so quick to skim through the API to see which method you
    > are looking for. Not the case here.


    Again, you can find it in the library reference:
    http://python.org/doc/current/lib/typesmapping.html

    Although it isn't clear until you've read that that dictionaries are a
    "mapping type", if you look in the index you'll find that both "dictionary
    object" and "dictionary type, operations on" point you to that section.

    I think you probably want to familiarise yourself with all of section 2 of
    the Library Reference.

    -Andrew.
     
    Andrew Bennetts, Oct 26, 2003
    #10
  11. In article <>,
    Andrew Bennetts <> wrote:

    > On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:
    > > type(i) == "<type 'float'>"
    > > this always returns false. How come?
    > > type(i)returns <type 'float'> if i is a float so why isn't == working?

    >
    > Because "<type 'float'>" is a string :)
    >
    > You want
    > import types
    > type(i) == types.FloatType
    >
    > or
    > type(i) == type(1.0)
    >
    > or in 2.2 and later you can simply do
    > type(i) == float


    But it's almost always preferable to do
    isinstance(i,float)
    because that allows subclasses of float to be used. If you really have
    to test whether i is an unsubclassed float,
    type(i) is float
    would be a better choice than
    type(i)==float
    as it more accurately expresses the intent that only the precise float
    type will be allowed.

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Oct 26, 2003
    #11
  12. In article <bnfjf4$10gat3$-berlin.de>,
    "Kill Bill" <> wrote:

    > Where can I find all the methods for Dictionaries? They list some here, but
    > is that all of them?
    > http://www.python.org/doc/2.3.2/tut/node7.html#SECTION007140000000000000000
    > I think it is, but I don't like how its written. I want it to be written
    > you know by method, constuctors, like in the Java API. Anyone know what I'm
    > talking about? Its so quick to skim through the API to see which method you
    > are looking for. Not the case here.


    Try typing help(dict) to the Python interpreter.

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
     
    David Eppstein, Oct 26, 2003
    #12
  13. Kill Bill

    Peter Hansen Guest

    Emile van Sebille wrote:
    >
    > "Kill Bill" <> wrote in message
    > news:bnfbuj$10f3di$-berlin.de...
    > > type(i) == "<type 'float'>"

    >
    > This compares the current type of i to type("string"), which is <type
    > 'str'>.


    (Minor correction) Actually it compares it to the actual string
    containing the letters "<type 'float'>", which is of course going
    to get one nowhere.

    The above would have worked if the OP had used

    repr(type(i)) == "<type 'float'>"

    but that is the absolute worst way of doing this...

    -Peter
     
    Peter Hansen, Oct 26, 2003
    #13
  14. Kill Bill

    John J. Lee Guest

    "Kill Bill" <> writes:
    > "KefX" <> wrote in message
    > news:...

    [...]
    > > Use isinstance(), like this:

    [...]
    > Can you tell me where you found that method in the docs? I'm having trouble

    [...]

    Rule of thumb: If you can't find it, it's in section 2 of the library docs.


    John
     
    John J. Lee, Oct 26, 2003
    #14
  15. In article <>,
    David Eppstein <> wrote:
    >In article <bnfjf4$10gat3$-berlin.de>,
    > "Kill Bill" <> wrote:
    >
    >> Where can I find all the methods for Dictionaries? They list some here, but
    >> is that all of them?
    >> http://www.python.org/doc/2.3.2/tut/node7.html#SECTION007140000000000000000
    >> I think it is, but I don't like how its written. I want it to be written
    >> you know by method, constuctors, like in the Java API. Anyone know what I'm
    >> talking about? Its so quick to skim through the API to see which method you
    >> are looking for. Not the case here.

    >
    >Try typing help(dict) to the Python interpreter.

    .
    .
    .
    Also
    dir(dict)
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Oct 26, 2003
    #15
    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. Sathyaish
    Replies:
    2
    Views:
    592
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    566
    Henry S. Thompson
    Dec 1, 2005
  3. Edward Rutherford

    Comparing fp types for equality

    Edward Rutherford, Dec 20, 2011, in forum: C Programming
    Replies:
    12
    Views:
    426
    Rui Maciel
    Dec 20, 2011
  4. Jason Friedman

    Comparing types

    Jason Friedman, Feb 17, 2013, in forum: Python
    Replies:
    4
    Views:
    118
    Chris Angelico
    Feb 18, 2013
  5. Terry Reedy

    Re: Comparing types

    Terry Reedy, Feb 17, 2013, in forum: Python
    Replies:
    0
    Views:
    127
    Terry Reedy
    Feb 17, 2013
Loading...

Share This Page