How to detect what type a variable is?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leandro Ardissone

    #1

    How to detect what type a variable is?

    Hi,

    I want to know what type is a variable.
    For example, I get the contents of an xml but some content is a list or
    a string, and I need to know what type it is.

    Thanks

  • Grant Edwards

    #2
    Re: How to detect what type a variable is?

    On 2006-11-29, Leandro Ardissone <lardissone@gma il.comwrote:
    I want to know what type is a variable. For example, I get the
    contents of an xml but some content is a list or a string, and
    I need to know what type it is.
    >>x = 'asdf'
    >>type(x)
    <type 'str'>
    >>i = 0
    >>type(i)
    <type 'int'>
    >>>
    --
    Grant Edwards grante Yow! I Know A Joke!!
    at
    visi.com

    Comment

    • Neil Cerutti

      #3
      Re: How to detect what type a variable is?

      On 2006-11-29, Grant Edwards <grante@visi.co mwrote:
      On 2006-11-29, Leandro Ardissone <lardissone@gma il.comwrote:
      >
      >I want to know what type is a variable. For example, I get the
      >contents of an xml but some content is a list or a string, and
      >I need to know what type it is.
      >
      >>>x = 'asdf'
      >>>type(x)
      ><type 'str'>
      >>>i = 0
      >>>type(i)
      ><type 'int'>
      That makes me wonder how he manages to store Python objects in
      xml.

      --
      Neil Cerutti

      Comment

      • SeanDavis12

        #4
        Re: How to detect what type a variable is?


        Leandro Ardissone wrote:
        Hi,
        >
        I want to know what type is a variable.
        For example, I get the contents of an xml but some content is a list or
        a string, and I need to know what type it is.
        type(variable)

        Sean

        Comment

        • Leandro Ardissone

          #5
          Re: How to detect what type a variable is?

          great, thanks

          And how I can compare this "<type 'str'>" output ?
          I want to decide what to do if the var is an string and what to do if
          not..

          Tried with:
          if type(artistList ) == "<type 'list'>":

          and
          if type(artistList ) == "list":

          but nothing..

          On Nov 29, 12:41 pm, Grant Edwards <gra...@visi.co mwrote:
          On 2006-11-29, Leandro Ardissone <lardiss...@gma il.comwrote:
          >
          >
          >
          I want to know what type is a variable. For example, I get the
          contents of an xml but some content is a list or a string, and
          I need to know what type it is.
          >x = 'asdf'
          >type(x)
          <type 'str'>
          >i = 0
          >type(i)
          <type 'int'>--
          Grant Edwards grante Yow! I Know A Joke!!
          at
          visi.com

          Comment

          • Leandro Ardissone

            #6
            Re: How to detect what type a variable is?

            Thanks,

            I don't store Python objects in xml, but I get an object from a library
            that parses xml and converts it to objects.


            On Nov 29, 12:43 pm, Neil Cerutti <horp...@yahoo. comwrote:
            On 2006-11-29, Grant Edwards <gra...@visi.co mwrote:
            >
            On 2006-11-29, Leandro Ardissone <lardiss...@gma il.comwrote:
            >
            I want to know what type is a variable. For example, I get the
            contents of an xml but some content is a list or a string, and
            I need to know what type it is.
            >
            >>x = 'asdf'
            >>type(x)
            <type 'str'>
            >>i = 0
            >>type(i)
            <type 'int'>That makes me wonder how he manages to store Python objects in
            xml.
            >
            --
            Neil Cerutti

            Comment

            • Roberto Bonvallet

              #7
              Re: How to detect what type a variable is?

              Leandro Ardissone wrote:
              And how I can compare this "<type 'str'>" output ?
              I want to decide what to do if the var is an string and what to do if
              not..
              >
              Tried with:
              if type(artistList ) == "<type 'list'>":
              >
              and
              if type(artistList ) == "list":
              >
              but nothing..
              type() doesn't return a string, it returns a type object.
              You should try this:

              if isinstance(arti stList, list):
              ...

              Cheers,
              --
              Roberto Bonvallet

              Comment

              • Leandro Ardissone

                #8
                Re: How to detect what type a variable is?

                great,

                that is just what I need!

                Thank you all!

                --
                Leandro Ardissone

                On Nov 29, 1:29 pm, Roberto Bonvallet <Roberto.Bonval ...@cern.ch>
                wrote:
                Leandro Ardissone wrote:
                And how I can compare this "<type 'str'>" output ?
                I want to decide what to do if the var is an string and what to do if
                not..
                >
                Tried with:
                if type(artistList ) == "<type 'list'>":
                >
                and
                if type(artistList ) == "list":
                >
                but nothing..type() doesn't return a string, it returns a type object.
                You should try this:
                >
                if isinstance(arti stList, list):
                ...
                >
                Cheers,
                --
                Roberto Bonvallet

                Comment

                • Daniel Klein

                  #9
                  Re: How to detect what type a variable is?

                  On 29 Nov 2006 08:25:35 -0800, "Leandro Ardissone"
                  <lardissone@gma il.comwrote:
                  >great, thanks
                  >
                  >And how I can compare this "<type 'str'>" output ?
                  >I want to decide what to do if the var is an string and what to do if
                  >not..
                  >
                  >Tried with:
                  >if type(artistList ) == "<type 'list'>":
                  >
                  >and
                  >if type(artistList ) == "list":
                  >
                  >but nothing..
                  Try it this way...
                  >>artistList = []
                  >>isinstance(ar tistList, list)
                  True
                  >>if isinstance(arti stList, list):
                  print "I'm a list."

                  I'm a list.
                  >>>

                  Daniel Klein

                  Comment

                  • eduardo.padoan@gmail.com

                    #10
                    Re: How to detect what type a variable is?


                    Leandro Ardissone wrote:
                    Hi,
                    >
                    I want to know what type is a variable.
                    For example, I get the contents of an xml but some content is a list or
                    a string, and I need to know what type it is.
                    You should try to treat it as a list, catch the exceptions raise when
                    it is a string (problably ValueError, TypeError ou Attribute error,
                    depends on what are you doing), and then treat it as a string. This is
                    the BAFP (better ask for forgiveness than permission) style, and is
                    more accepted in duck-typing languages like Python then LBYL (look
                    before you leep) style.

                    Comment

                    • Luis M. González

                      #11
                      Re: How to detect what type a variable is?


                      Leandro Ardissone wrote:
                      great, thanks
                      >
                      And how I can compare this "<type 'str'>" output ?
                      I want to decide what to do if the var is an string and what to do if
                      not..
                      >
                      Tried with:
                      if type(artistList ) == "<type 'list'>":
                      >
                      and
                      if type(artistList ) == "list":
                      >
                      but nothing..

                      You shouldn't enclose "list" inside quotes.
                      This is the correct way:

                      if type(artistList ) == list:
                      do something...

                      or as someone suggested:

                      if isinstance(l, list):
                      do something...


                      hope this helps.
                      Luis

                      Comment

                      • Grant Edwards

                        #12
                        Re: How to detect what type a variable is?

                        On 2006-11-29, eduardo.padoan@ gmail.com <eduardo.padoan @gmail.comwrote :
                        >
                        Leandro Ardissone wrote:
                        >Hi,
                        >>
                        >I want to know what type is a variable.
                        >For example, I get the contents of an xml but some content is a list or
                        >a string, and I need to know what type it is.
                        >
                        You should try to treat it as a list, catch the exceptions raise when
                        it is a string (problably ValueError, TypeError ou Attribute error,
                        depends on what are you doing),
                        As you said, it depends on what he's doing. The problem is
                        that strings act a lot like lists, and if he's using the subset
                        of list characteristics that are implimented by strings, there
                        won't _be_ an exception -- just wrong results.

                        In the past, I've almost alwasy had to look before I leap when
                        dealing with both strings and lists.

                        --
                        Grant Edwards grante Yow! How's it going in
                        at those MODULAR LOVE UNITS??
                        visi.com

                        Comment

                        • Tim Chase

                          #13
                          Re: How to detect what type a variable is?

                          >I want to know what type is a variable.
                          >
                          You should try to treat it as a list, catch the exceptions
                          raise when it is a string (problably ValueError, TypeError ou
                          Attribute error, depends on what are you doing), and then
                          treat it as a string. This is the BAFP (better ask for
                          forgiveness than permission) style

                          One might prefer to check for string-ness, as strings can
                          duck-type somewhat like lists:

                          my_list = ['my', 'brain', 'hurts']
                          my_string = 'Are you the brain specialist?'

                          for test in [my_list, my_string]:
                          try:
                          for thing in test:
                          process_list_it em(thing)
                          except Exception: #whatever flavor you want
                          process_string( thing) # not called because
                          #strings are iterable


                          This gives the potentially-surprising result of iterating over
                          my_string and calling process_list_it em() with each character in
                          the string, rather than raising some exception and calling
                          process_string( ). Python does the right thing, but it can be
                          confusing when the duck-typing syntax is identical for the two,
                          despite a desire to treat them differently...p erhaps a tree-like
                          list of lists and strings such as HTML/XML structure.

                          -tkc



                          Comment

                          • Eduardo \EdCrypt\ O. Padoan

                            #14
                            Re: How to detect what type a variable is?

                            >
                            One might prefer to check for string-ness, as strings can
                            duck-type somewhat like lists:
                            >
                            my_list = ['my', 'brain', 'hurts']
                            my_string = 'Are you the brain specialist?'
                            >
                            for test in [my_list, my_string]:
                            try:
                            for thing in test:
                            process_list_it em(thing)
                            except Exception: #whatever flavor you want
                            The exception should be the one that process_list_it em raises when it
                            receives a string instead of a list. if you want to treat strings and
                            list in different ways, maybe it means that you are doing different
                            operations on then, like appendind things to the list or whatever. If
                            not, than you maybe want to test the types.
                            process_string( thing) # not called because
                            #strings are iterable
                            What if you invert your code?


                            for test in [my_string, my_list]:
                            try:
                            process_string_ item(thing)
                            #suppose process_string_ item does some string operation on a
                            list and gets this
                            # exception - because if not, I see no meanning in distinguishing then
                            except ValueError:
                            for thing in test:
                            process_list_it em(thing)

                            But maybe you have a reason to do things to a string that could be
                            done to a list without raising an exception, but you dont want to do
                            this to *that* list. My sugestion was to think if there is another
                            way, and maybe you are right.


                            --
                            EduardoOPadoan (eopadoan->altavix::com )
                            Bookmarks: http://del.icio.us/edcrypt
                            Blog: http://edcrypt.blogspot.com
                            Jabber: edcrypt at jabber dot org
                            ICQ: 161480283
                            GTalk: eduardo dot padoan at gmail dot com
                            MSN: eopadoan at altavix dot com

                            Comment

                            • Dustan

                              #15
                              Re: How to detect what type a variable is?


                              Eduardo "EdCrypt" O. Padoan wrote:

                              One might prefer to check for string-ness, as strings can
                              duck-type somewhat like lists:

                              my_list = ['my', 'brain', 'hurts']
                              my_string = 'Are you the brain specialist?'

                              for test in [my_list, my_string]:
                              try:
                              for thing in test:
                              process_list_it em(thing)
                              except Exception: #whatever flavor you want
                              >
                              The exception should be the one that process_list_it em raises when it
                              receives a string instead of a list. if you want to treat strings and
                              list in different ways, maybe it means that you are doing different
                              operations on then, like appendind things to the list or whatever. If
                              not, than you maybe want to test the types.
                              >
                              process_string( thing) # not called because
                              #strings are iterable
                              >
                              What if you invert your code?
                              >
                              >
                              for test in [my_string, my_list]:
                              try:
                              process_string_ item(thing)
                              #suppose process_string_ item does some string operation on a
                              list and gets this
                              # exception - because if not, I see no meanning in distinguishing then
                              In case you had trouble reading the comments because it was too wide:
                              "suppose process_string_ item does some string operation on a list and
                              gets this exception - because if not, I see no meanning in
                              distinguishing then"

                              Has it occurred to you that perhaps the OP wants to do very different
                              operations depending on whether or not the item is of type str or list?
                              And since a list can in many cases act very similarly to a string, the
                              best way to distinguish would be a type check, especially since the OP
                              knows what tools are being used to receive the output.
                              except ValueError:
                              for thing in test:
                              process_list_it em(thing)
                              >
                              But maybe you have a reason to do things to a string that could be
                              done to a list without raising an exception, but you dont want to do
                              this to *that* list. My sugestion was to think if there is another
                              way, and maybe you are right.
                              >
                              >
                              --
                              EduardoOPadoan (eopadoan->altavix::com )
                              Bookmarks: http://del.icio.us/edcrypt
                              Blog: http://edcrypt.blogspot.com
                              Jabber: edcrypt at jabber dot org
                              ICQ: 161480283
                              GTalk: eduardo dot padoan at gmail dot com
                              MSN: eopadoan at altavix dot com

                              Comment

                              Working...