Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > type of simple object

Reply
Thread Tools

type of simple object

 
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      02-01-2005
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

 
Reply With Quote
 
 
 
 
Jacek Generowicz
Guest
Posts: n/a
 
      02-01-2005
"" <> 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

 
Reply With Quote
 
 
 
 
ech0
Guest
Posts: n/a
 
      02-01-2005
use the type function!

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

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

<type 'int'>

 
Reply With Quote
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      02-01-2005
The result <type 'str'>
How can I check it since it is not a string right?

Pujo

 
Reply With Quote
 
Jacek Generowicz
Guest
Posts: n/a
 
      02-01-2005
"" <> 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.
 
Reply With Quote
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      02-01-2005
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

 
Reply With Quote
 
Jacek Generowicz
Guest
Posts: n/a
 
      02-01-2005
"" <> 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.
 
Reply With Quote
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      02-01-2005
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)?

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-01-2005
> 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
 
Reply With Quote
 
Jacek Generowicz
Guest
Posts: n/a
 
      02-01-2005
"" <> 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]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"gridview Object does not match target type" error during binding collection of different type object gui.besse@gmail.com ASP .Net 2 08-06-2006 03:12 PM
Inheritance Problem from a simple base type to a structured complicated type mflll@wiu.edu XML 0 07-27-2006 12:26 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments