472,976 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

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 3011
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.