type of simple object

Discussion in 'Python' started by ajikoe@gmail.com, Feb 1, 2005.

  1. Guest

    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
     
    , Feb 1, 2005
    #1
    1. Advertising

  2. "" <> writes:

    > 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



    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
     
    Jacek Generowicz, Feb 1, 2005
    #2
    1. Advertising

  3. ech0 Guest

    use the type function!

    >>> s = 'test'
    >>> i = 25
    >>> type(s)

    <type 'str'>
    >>> type(i)

    <type 'int'>
     
    ech0, Feb 1, 2005
    #3
  4. Guest

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

    Pujo
     
    , Feb 1, 2005
    #4
  5. "" <> writes:

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


    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.
     
    Jacek Generowicz, Feb 1, 2005
    #5
  6. Guest

    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
     
    , Feb 1, 2005
    #6
  7. "" <> writes:

    > 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.


    Which member? A list can have many members ... or none at all.

    > That's why I need to know the type of my data in "something".


    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.

    > By the way I am new in python, I heard that it has a MatLab
    > capabilities, How good is that?


    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.
     
    Jacek Generowicz, Feb 1, 2005
    #7
  8. Guest

    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)?
     
    , Feb 1, 2005
    #8
  9. > Can you tell me how can I check if an object is a sequence (you are
    > right, this is actually what I want)?


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

    Diez B. Roggisch
     
    Diez B. Roggisch, Feb 1, 2005
    #9
  10. "" <> writes:

    > 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)?


    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:

    >>> def f(seq, something):

    .... try:
    .... number = sum(something)
    .... except TypeError:
    .... number = something
    .... return [number*item for item in seq]
    ....
    >>> f([1,2,3], 4)

    [4, 8, 12]
    >>> f([1,2,3], [1,2,3])

    [6, 12, 18]
    >>> f([1,2,3], (1,2,3))

    [6, 12, 18]
    >>> f([1,2,3], {1:'one', 2:'two', 3:'three'})

    [6, 12, 18]
     
    Jacek Generowicz, Feb 1, 2005
    #10
  11. a écrit :
    > 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".


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

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

    This function will, recursively multiply a nested list of numbers by
    "factor" ...

    >
    > 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.....


    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.

    >
    >
    > Sincerely Yours,
    > pujo
    >


    Pierre
     
    Pierre Barbier de Reuille, Feb 2, 2005
    #11
  12. Steve Holden Guest

    Pierre Barbier de Reuille wrote:

    > a écrit :
    >
    >> 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".

    >
    >
    > As stated by another comment, I would do something like :
    >
    > def multiply(object, factor):
    > try:
    > return [ multiply(i,factor) for i in object ]
    > except TypeError:
    > return object*factor
    >
    > This function will, recursively multiply a nested list of numbers by
    > "factor" ...
    >

    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/
     
    Steve Holden, Feb 2, 2005
    #12
  13. Steve Holden a écrit :
    > Pierre Barbier de Reuille wrote:
    >
    >> a écrit :
    >>
    >>> 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".

    >>
    >>
    >>
    >> As stated by another comment, I would do something like :
    >>
    >> def multiply(object, factor):
    >> try:
    >> return [ multiply(i,factor) for i in object ]
    >> except TypeError:
    >> return object*factor
    >>
    >> This function will, recursively multiply a nested list of numbers by
    >> "factor" ...
    >>

    > 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.


    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

    >
    > regards
    > Steve
     
    Pierre Barbier de Reuille, Feb 2, 2005
    #13
  14. wrote:
    > 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
    >

    If you are looking for MatLab like facilities you might consider
    numarray, available from sourceforge.

    Colin W.
     
    Colin J. Williams, Feb 2, 2005
    #14
    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. heyo
    Replies:
    3
    Views:
    913
    Dan Pop
    Apr 1, 2004
  2. pete
    Replies:
    4
    Views:
    797
    Dan Pop
    Apr 2, 2004
  3. Replies:
    0
    Views:
    391
  4. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    908
    Yevgen Muntyan
    Feb 13, 2007
  5. kj
    Replies:
    5
    Views:
    411
Loading...

Share This Page