views:

8878

answers:

7

I want to see the type of a variabe whether it is unsigned 32 bit,signed 16 bit etc. How to view...

+3  A: 

You may be looking for the type() function.

See the examples below, but there's no "unsigned" type in Python just like Java.

Positive integer:

>>> v = 10
>>> type(v)
<type 'int'>

Large positive integer:

>>> v = 100000000000000
>>> type(v)
<type 'long'>

Negative integer:

>>> v = -10
>>> type(v)
<type 'int'>

Literal sequence of characters:

>>> v = 'hi'
>>> type(v)
<type 'str'>
artknish
we will get those typesI asked for unsigned int,signed int etc
+2  A: 
print type(variable_name)

I also highly recommend the IPython interactive interpreter when dealing with questions like this. It lets you type variable_name? and will return a whole list of information about the object including the type and the doc string for the type.

e.g.

In [9]: var = 123

In [10]: var?
Type:    int
Base Class: <type 'int'>
String Form:    123
Namespace:  Interactive
Docstring:
    int(x[, base]) -> integer

    Convert a string or number to an integer, if possible.  A floating point
    argument will be truncated towards zero (this does not include a string
    representation of a floating point number!)  When converting a string, use
    the optional base.  It is an error to supply a base when converting a
    non-string. If the argument is outside the integer range a long object
    will be returned instead.
Lawrence Johnston
I asked for unsigned int,signed int etc
+7  A: 

The question is somewhat ambiguous -- I'm not sure what you mean by "view". If you are trying to query the type of a native Python object, @atzz's answer will steer you in the right direction.

However, if you are trying to generate Python objects that have the semantics of primitive C-types, (such as uint32_t, int16_t), use the struct module. You can determine the number of bits in a given C-type primitive thusly:

>>> struct.calcsize('c') # char
1
>>> struct.calcsize('h') # short
2
>>> struct.calcsize('i') # int
4
>>> struct.calcsize('l') # long
4

This is also reflected in the array module, which can make arrays of these lower-level types:

>>> array.array('c').itemsize # char
1

The maximum integer supported (Python 2's int) is given by sys.maxint.

>>> import sys, math
>>> math.ceil(math.log(sys.maxint, 2)) + 1 # Signedness
32.0

There is also sys.getsizeof, which returns the actual size of the Python object in residual memory:

>>> a = 5
>>> sys.getsizeof(a) # Residual memory.
12

For float data and precision data, use sys.float_info:

>>> sys.float_info
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)
cdleary
The question, as I understand it, asks about querying the type of a "variable" in Python. Your answer is generally correct but off-topic.
ΤΖΩΤΖΙΟΥ
Considering there are no "variables" in Python, only identifiers/bindings, I gave the question my best shot -- thought this might be the answer that the OP was looking for.
cdleary
But point taken, he does seem to pose it as a "querying" question rather than a "using" question...
cdleary
+3  A: 

Do you mean in python or using ctypes?

In the first case, you simply cannot - because python does not have signed/unsigned, 16/32 bit integers.

In the second case, you can use type():

>>> import ctypes
>>> a = ctypes.c_uint() # unsigned int
>>> type(a)
<class 'ctypes.c_ulong'>

For more reference on ctypes, an its type, see official documentation.

Roberto Liffredo
+8  A: 

Python doesn't have such types as you describe. There are two types used to represent integral values: int, which corresponds to platform's int type in C, and long, which is an arbitrary precision integer (i.e. it grows as needed and doesn't have an upper limit). ints are silently converted to long if an expression produces result which cannot be stored in int.

atzz
Just to clarify: it doesn't have the types available for *native* Python objects, but there are native Python *wrappers* in the `struct` and `array` modules.
cdleary
@cdleary: the question comes from a person who doesn't understand yet Python objects, nor names. Python doesn't have variables in the Basic or C sense. And how do you create a "variable" (not an array of size 1) of type unsigned int 16 using struct?
ΤΖΩΤΖΙΟΥ
@ΤΖΩΤΖΙΟΥ: Assuming that a uint16_t is an unsigned short on your machine: `import struct; s = struct.Struct('H'); assert s.size == 2; print s`
cdleary
It's also possible that your machine has no `uint16_t` -- you would have to do queries via struct.calcsize to determine which bitwidths are available.
cdleary
@cdleary: what's the use of the s object in your example? If it's a wrapper for a uint16_t, what's the uint16_t value? What's the value of s+1? Please do not underestimate anyone, including yourself. Thanks in advance.
ΤΖΩΤΖΙΟΥ
@ΤΖΩΤΖΙΟΥ: Re: underestimation - I think we may have a communication breakdown here - I'm just trying to answer questions in a way that might be helpful to people. Even if the OP meant something different by his question, people may interpret it the way I have and look for a corresponding answer.
cdleary
@ΤΖΩΤΖΙΟΥ: The answer to your question is that you'd have to turn it Python integer <-> byte stream for it to be useful to you, which I think you were hinting at: s.pack(16) yields a byte stream '\x10\x00', s.unpack('\x10\x00') yields 16.
cdleary
+17  A: 

Python doesn't have the same types as C/C++, which appears to be your question.

Try this:

>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123456789L
>>> type(i)
<type 'long'>
>>> type(i) is long
True
>>> i = 123.456
>>> type(i)
<type 'float'>
>>> type(i) is float
True

The distinction between int and long goes away in Python 3.0, though.

gregjor
+4  A: 

It really depends on what level you mean. In Python 2.x, there are two integer types, int (constrained to sys.maxint) and long (unlimited precision), for historical reasons. In Python code, this shouldn't make a bit of difference because the interpreter automatically converts to long when a number is too large. If you want to know about the actual data types used in the underlying interpreter, that's implementation dependent. (CPython's are located in Objects/intobject.c and Objects/longobject.c.) To find out about the systems types look at cdleary answer for using the struct module.

Benjamin Peterson