473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Comparing variable types

type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?
Jul 18 '05 #1
14 8634
>type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?


Use isinstance(), like this:
isinstance(i, float)
This will return True if i is a float, False if not.

- Kef
Jul 18 '05 #2

"Kill Bill" <bi**@kill.com> wrote in message
news:bn*************@ID-198839.news.uni-berlin.de...
type(i) == "<type 'float'>"
This compares the current type of i to type("string"), which is <type
'str'>.

You can do:
i = 1.3
type(i) == "<type 'float'>"
False str(type(i)) == "<type 'float'>" True

this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?

What you probably want is:
import types
type(i) == types.FloatType True


Emile van Sebille
em***@fenx.com


Jul 18 '05 #3
Can you tell me where you found that method in the docs? I'm having trouble
navigating them, the java docs are so much easier to look at.

"KefX" <ke**********@aol.comNOSPAM> wrote in message
news:20***************************@mb-m07.aol.com...
type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?


Use isinstance(), like this:
isinstance(i, float)
This will return True if i is a float, False if not.

- Kef

Jul 18 '05 #4
>What you probably want is:
import types
type(i) == types.FloatTypeTrue


Using isinstance() as I described earlier is probably better because I think it
works better with the idea of unifying types and classes.

- Kef
Jul 18 '05 #5
>Can you tell me where you found that method in the docs?

It's a builtin, meaning it's in module __builtin__. You'll find them in the
library reference near the top under "built-in functions" or some such.

- Kef
Jul 18 '05 #6
On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:
type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?


Because "<type 'float'>" is a string :)

You want
import types
type(i) == types.FloatType

or
type(i) == type(1.0)

or in 2.2 and later you can simply do
type(i) == float

-Andrew.
Jul 18 '05 #7
On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:
Can you tell me where you found that method in the docs? I'm having trouble
navigating them, the java docs are so much easier to look at.


Library Reference, section 2.1: Built-in Functions
http://python.org/doc/current/lib/built-in-funcs.html

-Andrew.
Jul 18 '05 #8
Where can I find all the methods for Dictionaries? They list some here, but
is that all of them?
http://www.python.org/doc/2.3.2/tut/...00000000000000
I think it is, but I don't like how its written. I want it to be written
you know by method, constuctors, like in the Java API. Anyone know what I'm
talking about? Its so quick to skim through the API to see which method you
are looking for. Not the case here.

"Andrew Bennetts" <an***************@puzzling.org> wrote in message
news:ma************************************@python .org...
On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:
Can you tell me where you found that method in the docs? I'm having trouble navigating them, the java docs are so much easier to look at.


Library Reference, section 2.1: Built-in Functions
http://python.org/doc/current/lib/built-in-funcs.html

-Andrew.

Jul 18 '05 #9
On Sun, Oct 26, 2003 at 12:44:20AM -0400, Kill Bill wrote:
Where can I find all the methods for Dictionaries? They list some here, but
is that all of them?
http://www.python.org/doc/2.3.2/tut/...00000000000000
I think it is, but I don't like how its written. I want it to be written
you know by method, constuctors, like in the Java API. Anyone know what I'm
talking about? Its so quick to skim through the API to see which method you
are looking for. Not the case here.


Again, you can find it in the library reference:
http://python.org/doc/current/lib/typesmapping.html

Although it isn't clear until you've read that that dictionaries are a
"mapping type", if you look in the index you'll find that both "dictionary
object" and "dictionary type, operations on" point you to that section.

I think you probably want to familiarise yourself with all of section 2 of
the Library Reference.

-Andrew.
Jul 18 '05 #10
In article <bn*************@ID-198839.news.uni-berlin.de>,
"Kill Bill" <bi**@kill.com> wrote:
Where can I find all the methods for Dictionaries? They list some here, but
is that all of them?
http://www.python.org/doc/2.3.2/tut/...00000000000000
I think it is, but I don't like how its written. I want it to be written
you know by method, constuctors, like in the Java API. Anyone know what I'm
talking about? Its so quick to skim through the API to see which method you
are looking for. Not the case here.


Try typing help(dict) to the Python interpreter.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #11
In article <ma************************************@python.org >,
Andrew Bennetts <an***************@puzzling.org> wrote:
On Sat, Oct 25, 2003 at 10:36:03PM -0400, Kill Bill wrote:
type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?


Because "<type 'float'>" is a string :)

You want
import types
type(i) == types.FloatType

or
type(i) == type(1.0)

or in 2.2 and later you can simply do
type(i) == float


But it's almost always preferable to do
isinstance(i,float)
because that allows subclasses of float to be used. If you really have
to test whether i is an unsubclassed float,
type(i) is float
would be a better choice than
type(i)==float
as it more accurately expresses the intent that only the precise float
type will be allowed.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #12
Emile van Sebille wrote:

"Kill Bill" <bi**@kill.com> wrote in message
news:bn*************@ID-198839.news.uni-berlin.de...
type(i) == "<type 'float'>"


This compares the current type of i to type("string"), which is <type
'str'>.


(Minor correction) Actually it compares it to the actual string
containing the letters "<type 'float'>", which is of course going
to get one nowhere.

The above would have worked if the OP had used

repr(type(i)) == "<type 'float'>"

but that is the absolute worst way of doing this...

-Peter
Jul 18 '05 #13
"Kill Bill" <bi**@kill.com> writes:
"KefX" <ke**********@aol.comNOSPAM> wrote in message
news:20***************************@mb-m07.aol.com... [...]
Use isinstance(), like this:

[...] Can you tell me where you found that method in the docs? I'm having trouble

[...]

Rule of thumb: If you can't find it, it's in section 2 of the library docs.
John
Jul 18 '05 #14
In article <ep****************************@news.service.uci.e du>,
David Eppstein <ep******@ics.uci.edu> wrote:
In article <bn*************@ID-198839.news.uni-berlin.de>,
"Kill Bill" <bi**@kill.com> wrote:
Where can I find all the methods for Dictionaries? They list some here, but
is that all of them?
http://www.python.org/doc/2.3.2/tut/...00000000000000
I think it is, but I don't like how its written. I want it to be written
you know by method, constuctors, like in the Java API. Anyone know what I'm
talking about? Its so quick to skim through the API to see which method you
are looking for. Not the case here.


Try typing help(dict) to the Python interpreter.

Jul 18 '05 #15

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

Similar topics

1
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
5
by: Joe Black | last post by:
Hi all, Using Windows CP Pro VS .net 2005 I'm creating an app that allows user to extend its functionality by installing plug in modules, these modules support an interface I have created...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: Mike J | last post by:
Hi..need help comparing types example method istypeof(object someobj) { if (someobj=int32) } ya kinda get my idea here.... MJ
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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
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
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...

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.