473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determine variable type

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.****
Aug 18 '05 #1
4 3037
MC******@seminolecountyfl.gov writes:
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'
Aug 18 '05 #2
Paul Rubin wrote:
MC******@seminolecountyfl.gov writes:
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'


or

x = 5
isinstance(x, int)
True

BranoZ

Aug 18 '05 #3
MC******@seminolecountyfl.gov wrote:
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 Da*****@Acm.Org

Aug 18 '05 #4
MC******@seminolecountyfl.gov wrote:
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!'

Aug 19 '05 #5

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

Similar topics

17
6100
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
2575
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't...
14
24417
by: J. Jones | last post by:
Suppose the following: class MyContainer : System.Collections.CollectionBase { //... } (where CollectionBase implements IList, ICollection) How do I determine if a type (such as...
3
1153
by: M Shafaat | last post by:
Hi! I want to write C# code by which I can determine the name of an object and/or variable at run-time. VS design windows does it when you drag a control onto your form. It uses the name of the...
16
3401
by: Jm | last post by:
Hi All Is it possible to determine who is logged onto a machine from inside a service using code in vb.net ? I have found some code that seems to work under vb6, but doesnt under .NET ? Any help...
1
1548
by: tshad | last post by:
I have some code to go through a session collection for my error page routine and I get an error on my objects that I store in session variables. Dim strName as String Dim iLoop as Integer ...
5
6823
by: MLH | last post by:
Suppose MyName, a string variable equals "frmEnterClients". How can I determine ... 1) if frmEnterClients exists as an object in the database? 2) what type of object is it (tbl, qry, frm, rpt,...
8
1694
by: Ole | last post by:
If I define a class and create a instant of it like e.g.: UserClass instantName = new UserClass(); how do I then determine the defined name "instantName" in the UserClass e.g. in a method (or...
29
5052
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
0
7126
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,...
0
7168
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,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
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...
0
4595
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...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
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 ...

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.