determine variable type

M

MCollins

trying to determine a variable type, specifically that a variable is an
integer.

i tried using type(var) but that only seemed to produce a response in the
command line.

is there a built in python function to determine if a variable is an
integer?




--****Florida has a very broad Public Records Law. Virtually all written
communications to or from State and Local Officials and employees are
public records available to the public and media upon request. Seminole
County policy does not differentiate between personal and business emails.
E-mail sent on the County system will be considered public and will only
be withheld from disclosure if deemed confidential pursuant to State
Law.****
 
P

Paul Rubin

i tried using type(var) but that only seemed to produce a response in the
command line.

is there a built in python function to determine if a variable is an
integer?

type(var) returns the type. For example:

if type(x) == type(3):
print 'x is an integer'
 
S

Scott David Daniels

trying to determine a variable type, specifically that a variable is an
integer.

i tried using type(var) but that only seemed to produce a response in the
command line.

is there a built in python function to determine if a variable is an
integer?

if isinstance(var, (int, long)):
print 'var (%r) is an integer' % var
elif isinstance(var, float) and int(var) == var:
print 'var (%r) is a float that could be an integer' % var
else:
print 'var (%r) is not an integer' % var

--Scott David Daniels
Scott (e-mail address removed)
 
M

Magnus Lycka

trying to determine a variable type, specifically that a variable is an
integer.

i tried using type(var) but that only seemed to produce a response in the
command line.

You mean that if you do "type(var)" at the Python prompt, it gives
you a reply, but if you have a line with just that in a script, it
doesn't?

That has nothing to do with type checks. All Python expressions in
Python work like that. In interactive use, unused results are echoed
to the screen, but in scripts, they are not. How would you expect
the echoed value to be useful in your program?

In a script, you need to take care of the output of an expression
to get any use from it. A standalone expression on a line of its
own is legal Python code, but the result is thrown away... E.g.

var = 6
type(var) # Works, but meaningless, noone will know what happened
print type(var) # Like the interactive use (more or less)
var_type = type(var) # Now you have a variable containing the result
# of the expression. You can use that later
if type(var) == int: # You can use the result in another construct...
print "%i is an integer!" % var

But don't worry so much about type checking. That's typically not
so important in Python as is is in most other languages, in fact, if
you try to handle types too strictly in Python, you are throwing away
a lot of the benefits of Python's dynamic features.

Perhaps your code is useful with other types than integers, such as
the new decimal type or some upcoming money type. That typecheck might
just make it impossible to use your code in some completely meaningful
way. If you just did nothing, your code would probably throw an
exception if 'var' had a type that didn't make sense, and handling
that exception is probably not more difficult than to handle whatever
action you planned to take if var wasn't an int...

Python is much stricter in its type handling than e.g. C++, so don't
worry so much. Trying to add a string to an int etc, will raise a
sensible exception. It won't lead to any program crash or give any
bizarre result to your calculations.

If you're sanitizing user input, that's a good thing of course, but
then the safe thing would be to read strings (e.g. via raw_input) and
then inspect the string before you evaluate it or cast it to something
else. Actually, int() does this pretty well, so you could just do:

..while 1: # Loop until broken out of...
.. response = raw_input('Number please: ')
.. try:
.. var = int(response)
.. break
.. except ValueError:
.. print response, 'isn't a number!'
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top