type of simple object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajikoe@gmail.com

    #1

    type of simple object

    Hi,

    How do I know type of simple object is tuple or list or integer, for
    example my function should understand what is the object type passed in
    its argument

    Pujo

  • ech0

    #2
    Re: type of simple object

    use the type function!
    [color=blue][color=green][color=darkred]
    >>> s = 'test'
    >>> i = 25
    >>> type(s)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>> type(i)[/color][/color][/color]
    <type 'int'>

    Comment

    • Jacek Generowicz

      #3
      Re: type of simple object

      "ajikoe@gmail.c om" <ajikoe@gmail.c om> writes:
      [color=blue]
      > How do I know type of simple object is tuple or list or integer, for
      > example my function should understand what is the object type passed in
      > its argument[/color]


      Answers ordered in decreasing degree of Pythonicity:

      1) You are mistaken, the function almost certainly should not care
      whether the object is a tuple or a list (or integer)[1].

      2) isinstance(obj, list) etc.

      3) type(obj)



      [1] You probably want to check whether the object is capable of doing
      whatever it is that you expect lists or tuples to do for you. For
      example:


      def total(object):
      try:
      return sum(object) # The list-or-tuple case
      except TypeError:
      return object # The integer case

      Comment

      • ajikoe@gmail.com

        #4
        Re: type of simple object

        The result <type 'str'>
        How can I check it since it is not a string right?

        Pujo

        Comment

        • Jacek Generowicz

          #5
          Re: type of simple object

          "ajikoe@gmail.c om" <ajikoe@gmail.c om> writes:
          [color=blue]
          > The result <type 'str'>
          > How can I check it since it is not a string right?[/color]

          It is a type, which is a first-class object in its own right.

          type('hello') == str

          However, I reiterate, you almost certainly don't really care about
          what the actual type is. To care about the actual type is to struggle
          against a fundamental feature of python: Duck Typing.

          Yes, I admit, there are situations in which you might really care
          about the actual type. However, given that you do not know how to
          check the type in Python, the chances are rather high that you are
          sufficienly new to Python to not realize that, typically, you need not
          (and should not) care about the actual type. The chances are that you
          are trying to program in a style which you learned in another
          language, and which not the most productive in Python.

          Comment

          • ajikoe@gmail.com

            #6
            Re: type of simple object

            Thank you guys.

            My function should multiply every element of a list, for example
            "something"
            and "something" can be an integer or another list.
            If it deals with integer than it is ok, but
            If it deals with list than it become false for example list*2 =
            listlist, and what I really want is to mutlitply its member.
            That's why I need to know the type of my data in "something" .

            By the way I am new in python, I heard that it has a MatLab
            capabilities, How good is that? Since It would be very nice when we can
            do what MatLab do in python.....


            Sincerely Yours,
            pujo

            Comment

            • Jacek Generowicz

              #7
              Re: type of simple object

              "ajikoe@gmail.c om" <ajikoe@gmail.c om> writes:
              [color=blue]
              > Thank you guys.
              >
              > My function should multiply every element of a list, for example
              > "something"
              > and "something" can be an integer or another list.
              > If it deals with integer than it is ok, but
              > If it deals with list than it become false for example list*2 =
              > listlist, and what I really want is to mutlitply its member.[/color]

              Which member? A list can have many members ... or none at all.
              [color=blue]
              > That's why I need to know the type of my data in "something" .[/color]

              No, you don't need to know its type at all. You need to know whether
              it is a sequence or not ... which is a far cry from knowing its type.
              [color=blue]
              > By the way I am new in python, I heard that it has a MatLab
              > capabilities, How good is that?[/color]

              Depends on what you mean by "MatLab capabilities". Matlab is highly
              matrix oriented, therefore it provides lots of fast matrix operations,
              and syntactic sugar for dealing with matrices. If you want to think of
              everything as a matrix, then you could well be disappointed by
              Python.

              There is a package for driving matlab from Python, which you might
              find interesting (it might even be what you meant by "MatLab
              capabilities"). Google is your friend.

              Comment

              • ajikoe@gmail.com

                #8
                Re: type of simple object

                Hello Jacek,

                Thanks for the answer,

                Can you tell me how can I check if an object is a sequence (you are
                right, this is actually what I want)?

                Comment

                • Diez B. Roggisch

                  #9
                  Re: type of simple object

                  > Can you tell me how can I check if an object is a sequence (you are[color=blue]
                  > right, this is actually what I want)?[/color]

                  read the docs for the module "types."
                  --
                  Regards,

                  Diez B. Roggisch

                  Comment

                  • Jacek Generowicz

                    #10
                    Re: type of simple object

                    "ajikoe@gmail.c om" <ajikoe@gmail.c om> writes:
                    [color=blue]
                    > Hello Jacek,
                    >
                    > Thanks for the answer,
                    >
                    > Can you tell me how can I check if an object is a sequence (you are
                    > right, this is actually what I want)?[/color]

                    You try to use it as a sequence. If it works, then it was a
                    sequence. If it was not a sequence, you handle the exception and do
                    something appropriate.

                    For example:
                    [color=blue][color=green][color=darkred]
                    >>> def f(seq, something):[/color][/color][/color]
                    .... try:
                    .... number = sum(something)
                    .... except TypeError:
                    .... number = something
                    .... return [number*item for item in seq]
                    ....[color=blue][color=green][color=darkred]
                    >>> f([1,2,3], 4)[/color][/color][/color]
                    [4, 8, 12][color=blue][color=green][color=darkred]
                    >>> f([1,2,3], [1,2,3])[/color][/color][/color]
                    [6, 12, 18][color=blue][color=green][color=darkred]
                    >>> f([1,2,3], (1,2,3))[/color][/color][/color]
                    [6, 12, 18][color=blue][color=green][color=darkred]
                    >>> f([1,2,3], {1:'one', 2:'two', 3:'three'})[/color][/color][/color]
                    [6, 12, 18]

                    Comment

                    • Pierre Barbier de Reuille

                      #11
                      Re: type of simple object

                      ajikoe@gmail.co m a écrit :[color=blue]
                      > Thank you guys.
                      >
                      > My function should multiply every element of a list, for example
                      > "something"
                      > and "something" can be an integer or another list.
                      > If it deals with integer than it is ok, but
                      > If it deals with list than it become false for example list*2 =
                      > listlist, and what I really want is to mutlitply its member.
                      > That's why I need to know the type of my data in "something" .[/color]

                      As stated by another comment, I would do something like :

                      def multiply(object , factor):
                      try:
                      return [ multiply(i,fact or) for i in object ]
                      except TypeError:
                      return object*factor

                      This function will, recursively multiply a nested list of numbers by
                      "factor" ...
                      [color=blue]
                      >
                      > By the way I am new in python, I heard that it has a MatLab
                      > capabilities, How good is that? Since It would be very nice when we can
                      > do what MatLab do in python.....[/color]

                      I think you are referring to the Numeric or the numarray modules. They
                      offer matric computations close to chat Matlab offers. "numarray" is the
                      newer version of "Numeric", but in case of small matrix, it performs
                      slower (for various reasons). Then, you can find lots of information on
                      the net concerning these two modules.
                      [color=blue]
                      >
                      >
                      > Sincerely Yours,
                      > pujo
                      >[/color]

                      Pierre

                      Comment

                      • Steve Holden

                        #12
                        Re: type of simple object

                        Pierre Barbier de Reuille wrote:
                        [color=blue]
                        > ajikoe@gmail.co m a écrit :
                        >[color=green]
                        >> Thank you guys.
                        >>
                        >> My function should multiply every element of a list, for example
                        >> "something"
                        >> and "something" can be an integer or another list.
                        >> If it deals with integer than it is ok, but
                        >> If it deals with list than it become false for example list*2 =
                        >> listlist, and what I really want is to mutlitply its member.
                        >> That's why I need to know the type of my data in "something" .[/color]
                        >
                        >
                        > As stated by another comment, I would do something like :
                        >
                        > def multiply(object , factor):
                        > try:
                        > return [ multiply(i,fact or) for i in object ]
                        > except TypeError:
                        > return object*factor
                        >
                        > This function will, recursively multiply a nested list of numbers by
                        > "factor" ...
                        >[/color]
                        As a matter of good practice it's usually considered unwise to shadow
                        names of system types like "dict" and "object", though there wouldn't be
                        any problems in this case except the infinite recursion. Which
                        definitely *would* be a problem.

                        regards
                        Steve
                        --
                        Meet the Python developers and your c.l.py favorites March 23-25
                        Come to PyCon DC 2005 http://www.python.org/pycon/2005/
                        Steve Holden http://www.holdenweb.com/

                        Comment

                        • Pierre Barbier de Reuille

                          #13
                          Re: type of simple object

                          Steve Holden a écrit :[color=blue]
                          > Pierre Barbier de Reuille wrote:
                          >[color=green]
                          >> ajikoe@gmail.co m a écrit :
                          >>[color=darkred]
                          >>> Thank you guys.
                          >>>
                          >>> My function should multiply every element of a list, for example
                          >>> "something"
                          >>> and "something" can be an integer or another list.
                          >>> If it deals with integer than it is ok, but
                          >>> If it deals with list than it become false for example list*2 =
                          >>> listlist, and what I really want is to mutlitply its member.
                          >>> That's why I need to know the type of my data in "something" .[/color]
                          >>
                          >>
                          >>
                          >> As stated by another comment, I would do something like :
                          >>
                          >> def multiply(object , factor):
                          >> try:
                          >> return [ multiply(i,fact or) for i in object ]
                          >> except TypeError:
                          >> return object*factor
                          >>
                          >> This function will, recursively multiply a nested list of numbers by
                          >> "factor" ...
                          >>[/color]
                          > As a matter of good practice it's usually considered unwise to shadow
                          > names of system types like "dict" and "object", though there wouldn't be
                          > any problems in this case except the infinite recursion. Which
                          > definitely *would* be a problem.[/color]

                          Oops ... indeed, I usually try not to do so ^_^
                          That's why I usually use "obj" more than "object" and that most of the
                          time I use a name more _on the topic_ ...

                          Thx for the correction :)

                          Pierre
                          [color=blue]
                          >
                          > regards
                          > Steve[/color]

                          Comment

                          • Colin J. Williams

                            #14
                            Re: type of simple object

                            ajikoe@gmail.co m wrote:[color=blue]
                            > Thank you guys.
                            >
                            > My function should multiply every element of a list, for example
                            > "something"
                            > and "something" can be an integer or another list.
                            > If it deals with integer than it is ok, but
                            > If it deals with list than it become false for example list*2 =
                            > listlist, and what I really want is to mutlitply its member.
                            > That's why I need to know the type of my data in "something" .
                            >
                            > By the way I am new in python, I heard that it has a MatLab
                            > capabilities, How good is that? Since It would be very nice when we can
                            > do what MatLab do in python.....
                            >
                            >
                            > Sincerely Yours,
                            > pujo
                            >[/color]
                            If you are looking for MatLab like facilities you might consider
                            numarray, available from sourceforge.

                            Colin W.

                            Comment

                            Working...