473,795 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 18 '05 #1
13 1685
use the type function!
s = 'test'
i = 25
type(s) <type 'str'> type(i)

<type 'int'>

Jul 18 '05 #2
"aj****@gmail.c om" <aj****@gmail.c om> 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

Jul 18 '05 #3
The result <type 'str'>
How can I check it since it is not a string right?

Pujo

Jul 18 '05 #4
"aj****@gmail.c om" <aj****@gmail.c om> 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.
Jul 18 '05 #5
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

Jul 18 '05 #6
"aj****@gmail.c om" <aj****@gmail.c om> 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.
Jul 18 '05 #7
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)?

Jul 18 '05 #8
> 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
Jul 18 '05 #9
"aj****@gmail.c om" <aj****@gmail.c om> 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]
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
4568
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
8
3656
by: Chris Smith | last post by:
Experience posters, I am an experienced vb/vb.net developer but having a bit of trouble converting a bit of code to C#. I have 3 projects in one solution. Trying to create a plug-in type framework. Project 1. Main winforms exe a. Has a reference to project 2
2
2135
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType(); That's simple. But to create any object knowing its object type on the fly is looking like a problem. I'll try to explain the idea.
4
2096
by: st_ev_fe | last post by:
Hi people, I've been doing C for about 7 years now. But I'm new to C++. I've decided that C++'s operator overloading could be very handy. I'm writing something much like auto_ptr, except for my own set of classes. Basically, it's a class that is supposed to be allocated automatically on the stack, and possesess one member which is a pointer to an object. Unlike auto_ptr, my object will actually call a lock or unlock function
6
1915
by: Picho | last post by:
Hi all. I have a webservice and a windows app. both of them reference the same class library called WebServiceTest.Core that defines a class called Class1. the webservice exposes a method that looks like this: public WebServiceTest.Core.Class1 GetClass1()
8
1508
by: andyjgw | last post by:
Hi all, hope someone can help here, I'm really stuck. Reading and googling like mad, to no avail so far... Basically, my problem *seems* to boil down to type conversion: Here's a sample web service I knocked up: <WebMethod()> _ Public Function HelloWorld() As myType
33
5084
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
9
4010
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the object - a variable (B) of the type "Type" which contains the type of the object in (A) (A should be of the type B) - a method which should return the object (A) as type (B)
21
5202
by: Nikolaus Rath | last post by:
Hello, Can someone explain to me the difference between a type and a class? After reading http://www.cafepy.com/article/python_types_and_objects/ it seems to me that classes and types are actually the same thing: - both are instances of a metaclass, and the same metaclass ('type') can instantiate both classes and types. - both can be instantiated and yield an "ordinary" object - I can even inherit from a type and get a class
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.