type of simple object

A

ajikoe

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
 
J

Jacek Generowicz

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
 
J

Jacek Generowicz

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

ajikoe

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
 
J

Jacek Generowicz

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

ajikoe

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

Diez B. Roggisch

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

Jacek Generowicz

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:
.... 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]
 
P

Pierre Barbier de Reuille

(e-mail address removed) 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
 
S

Steve Holden

Pierre said:
(e-mail address removed) 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
 
P

Pierre Barbier de Reuille

Steve Holden a écrit :
Pierre said:
(e-mail address removed) 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
 
C

Colin J. Williams

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top