Comparing variable types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kill Bill

    Comparing variable types

    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?


  • KefX

    #2
    Re: Comparing variable types

    >type(i) == "<type 'float'>"[color=blue]
    >this always returns false. How come?
    >type(i)retur ns <type 'float'> if i is a float so why isn't == working?[/color]

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

    - Kef

    Comment

    • Emile van Sebille

      #3
      Re: Comparing variable types


      "Kill Bill" <bill@kill.co m> wrote in message
      news:bnfbuj$10f 3di$1@ID-198839.news.uni-berlin.de...[color=blue]
      > type(i) == "<type 'float'>"[/color]

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

      You can do:[color=blue][color=green][color=darkred]
      >>> i = 1.3
      >>> type(i) == "<type 'float'>"[/color][/color][/color]

      False[color=blue][color=green][color=darkred]
      >>> str(type(i)) == "<type 'float'>"[/color][/color][/color]
      True

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


      What you probably want is:
      [color=blue][color=green][color=darkred]
      >>> import types
      >>> type(i) == types.FloatType[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Emile van Sebille
      emile@fenx.com




      Comment

      • Kill Bill

        #4
        Re: Comparing variable types

        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" <keflimarcusx@a ol.comNOSPAM> wrote in message
        news:2003102522 4446.20009.0000 0076@mb-m07.aol.com...[color=blue][color=green]
        > >type(i) == "<type 'float'>"
        > >this always returns false. How come?
        > >type(i)retur ns <type 'float'> if i is a float so why isn't == working?[/color]
        >
        > Use isinstance(), like this:
        > isinstance(i, float)
        > This will return True if i is a float, False if not.
        >
        > - Kef[/color]


        Comment

        • KefX

          #5
          Re: Comparing variable types

          >What you probably want is:[color=blue]
          >[color=green][color=darkred]
          >>>> import types
          >>>> type(i) == types.FloatType[/color][/color]
          >True[color=green][color=darkred]
          >>>>[/color][/color][/color]

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

          - Kef

          Comment

          • KefX

            #6
            Re: Comparing variable types

            >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

            Comment

            • Andrew Bennetts

              #7
              Re: Comparing variable types

              On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:[color=blue]
              > 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?[/color]

              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.


              Comment

              • Andrew Bennetts

                #8
                Re: Comparing variable types

                On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:[color=blue]
                > 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.[/color]

                Library Reference, section 2.1: Built-in Functions


                -Andrew.


                Comment

                • Kill Bill

                  #9
                  Re: Comparing variable types

                  Where can I find all the methods for Dictionaries? They list some here, but
                  is that all of them?

                  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" <andrew-pythonlist@puzz ling.org> wrote in message
                  news:mailman.10 3.1067137271.70 2.python-list@python.org ...[color=blue]
                  > On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:[color=green]
                  > > Can you tell me where you found that method in the docs? I'm having[/color][/color]
                  trouble[color=blue][color=green]
                  > > navigating them, the java docs are so much easier to look at.[/color]
                  >
                  > Library Reference, section 2.1: Built-in Functions
                  > http://python.org/doc/current/lib/built-in-funcs.html
                  >
                  > -Andrew.
                  >
                  >[/color]


                  Comment

                  • Andrew Bennetts

                    #10
                    Re: Comparing variable types

                    On Sun, Oct 26, 2003 at 12:44:20AM -0400, Kill Bill wrote:[color=blue]
                    > 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/...00000000000000
                    > 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.[/color]

                    Again, you can find it in the library reference:


                    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.


                    Comment

                    • David Eppstein

                      #11
                      Re: Comparing variable types

                      In article <bnfjf4$10gat3$ 1@ID-198839.news.uni-berlin.de>,
                      "Kill Bill" <bill@kill.co m> wrote:
                      [color=blue]
                      > 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/...00000000000000
                      > 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.[/color]

                      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

                      Comment

                      • David Eppstein

                        #12
                        Re: Comparing variable types

                        In article <mailman.102.10 67136355.702.py thon-list@python.org >,
                        Andrew Bennetts <andrew-pythonlist@puzz ling.org> wrote:
                        [color=blue]
                        > On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:[color=green]
                        > > 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?[/color]
                        >
                        > 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[/color]

                        But it's almost always preferable to do
                        isinstance(i,fl oat)
                        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

                        Comment

                        • Peter Hansen

                          #13
                          Re: Comparing variable types

                          Emile van Sebille wrote:[color=blue]
                          >
                          > "Kill Bill" <bill@kill.co m> wrote in message
                          > news:bnfbuj$10f 3di$1@ID-198839.news.uni-berlin.de...[color=green]
                          > > type(i) == "<type 'float'>"[/color]
                          >
                          > This compares the current type of i to type("string"), which is <type
                          > 'str'>.[/color]

                          (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

                          Comment

                          • John J. Lee

                            #14
                            Re: Comparing variable types

                            "Kill Bill" <bill@kill.co m> writes:[color=blue]
                            > "KefX" <keflimarcusx@a ol.comNOSPAM> wrote in message
                            > news:2003102522 4446.20009.0000 0076@mb-m07.aol.com...[/color]
                            [...][color=blue][color=green]
                            > > Use isinstance(), like this:[/color][/color]
                            [...][color=blue]
                            > Can you tell me where you found that method in the docs? I'm having trouble[/color]
                            [...]

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


                            John

                            Comment

                            • Cameron Laird

                              #15
                              Re: Comparing variable types

                              In article <eppstein-D9C542.23251125 102003@news.ser vice.uci.edu>,
                              David Eppstein <eppstein@ics.u ci.edu> wrote:[color=blue]
                              >In article <bnfjf4$10gat3$ 1@ID-198839.news.uni-berlin.de>,
                              > "Kill Bill" <bill@kill.co m> wrote:
                              >[color=green]
                              >> 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/...00000000000000
                              >> 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.[/color]
                              >
                              >Try typing help(dict) to the Python interpreter.[/color]

                              Comment

                              Working...