473,397 Members | 1,985 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,397 software developers and data experts.

A bit weird dictionary behavior

Hello,

just noticed this:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>{1: 2}
{1: 2}
>>{True: False}
{True: False}
>>{1: 2, True: False}
{1: False}

This must be because
>>True == 1 and True in {1: 2}
True

but it still doesn't feel exactly right. Would it be worth submitting a bug?

Cheers,
.peke
Sep 22 '08 #1
10 1039
On 22 Sep, 10:25, "Pekka Laukkanen" <p...@iki.fiwrote:
Hello,

just noticed this:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>{1: 2}
{1: 2}
>{True: False}
{True: False}
>{1: 2, True: False}

{1: False}

This must be because
>True == 1 and True in {1: 2}

True
That's exactly the reason!
but it still doesn't feel exactly right. Would it be worth submitting a bug?
I don't think it can be considered as a bug, for the reason you gave
above and because dictionary keys are by definition unique with
respect to equality.

Perhaps you could call it a "surprising feature" :)

--
Arnaud
Sep 22 '08 #2
Pekka Laukkanen:
but it still doesn't feel exactly right. Would it be worth submitting a bug?
It feels wrong because it is. In a tidier language (Pascal, Java, etc)
a boolean and an integer must be different types. Keeping booleans and
integers separated may avoid some bugs too (I don't know how many/
often).
But the Python language copies many things from C, where most things
are chosen for practical purposes and maximum efficiency (and from
much simpler Python implementations, where there was no boolean type,
so bools are grafted in), so it's not a bug, and I think it will not
be fixed soon (Python 3 was probably the last chance to change it, for
several years to come).
So you probably have to live with this illogical behavior.

On the other hand it has some little practical advantages, you can do:
sum(x == y for x in iterable)

That also equals to a more tidy:
sum(1 for x in iterable if x == y)

Regarding the dict, they are dynamically typed, but good programming
practice (that comes from experience of bugs that have bitten you)
tells you that generally it's better to be careful with the inserting
different types into a dict; often it's better to avoid doing it.

Bye,
bearophile
Sep 22 '08 #3
Tino Wildenhain:
Wouldn't
len([x for x in iterable if x==y])
or even shorter:
iterable.count(y)
not work and read better anyway?
The first version creates an actual list just to take its length,
think about how much memory it may use.
The second version requires the 'iterable' object to have a count()
method, and in general this is false.

even calculating with boolean values isn't neccessary
since 'and' and 'foo if bar else blub' are working much better
so the type coalescing
bool - int - float can really go away.
I don't understand.

Bye,
bearophile
Sep 22 '08 #4
On Sep 22, 3:43*pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
bearophileH...@lycos.com a écrit :
Pekka Laukkanen:
but it still doesn't feel exactly right. Would it be worth submitting a bug?
It feels wrong because it is. In a tidier language (Pascal, Java, etc)
a boolean and an integer must be different types.

Some would argue (and some did by the time Python grew a 'bool' type)
that what is wrong is to have a bool type in a language that already
have a wider definition of the truth value of an expression...
And some would argue that it was wrong to have such a wide definition
for the truth value of an expression in the first place...
Carl Banks
Sep 22 '08 #5
On 22.09.2008, Carl Banks <pa************@gmail.comwroted:
>but it still doesn't feel exactly right. Would it be worth submitting a bug?
It feels wrong because it is. In a tidier language (Pascal, Java, etc)
a boolean and an integer must be different types.

Some would argue (and some did by the time Python grew a 'bool' type)
that what is wrong is to have a bool type in a language that already
have a wider definition of the truth value of an expression...

And some would argue that it was wrong to have such a wide definition
for the truth value of an expression in the first place...
Just out of idle curiosity, what could be the alternatives? Not to evaluate
e.g. strings to "true"? Aren't such conventions as "whatever is not empty,
is 'true'" popular in dynamic langauges?

GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Nocturnal Infiltration and Accurate Killing
Sep 22 '08 #6
On Mon, 22 Sep 2008 07:35:50 -0700, bearophileHUGS wrote:
Tino Wildenhain:
>Wouldn't
len([x for x in iterable if x==y])
or even shorter:
iterable.count(y)
not work and read better anyway?

The first version creates an actual list just to take its length, think
about how much memory it may use.
For many iterables, the amount of memory is not excessive and the
increase in readability of len() is to be preferred over the side-effect
of sum(1 for...).

But sure, in general you shouldn't try to count the number of items in an
arbitrary iterable unless you know how much time and resources it will
end up using. That's why I don't think len() should support arbitrary
iterables.
>even calculating with boolean values isn't neccessary since 'and' and
'foo if bar else blub' are working much better so the type coalescing
bool - int - float can really go away.

I don't understand.

I think Tino means that you don't need to cast items to bool since you
can use ints, floats etc. directly as truth values.

--
Steven
Sep 22 '08 #7
Grzegorz Staniak wrote:
On 22.09.2008, Carl Banks <pa************@gmail.comwroted:
>>Some would argue (and some did by the time Python grew a 'bool' type)
that what is wrong is to have a bool type in a language that already
have a wider definition of the truth value of an expression...
And some would argue that it was wrong to have such a wide definition
for the truth value of an expression in the first place...
An alternate viewpoint is that only True and False 'have' a truth value.
So whenever the interpreter *needs* a truth value for conditional and
logical operations, and it has something else, it implicitly calls
bool(ob) to get one. This, or possibly a shortcut version thereof, is
what a Python interpreter effectively does.

From this viewpoint, objecters would instead have to argue that it is
wrong to have such implicit calls and that programmers should have to
put them in explicitly.
Just out of idle curiosity, what could be the alternatives? Not to evaluate
e.g. strings to "true"? Aren't such conventions as "whatever is not empty,
is 'true'" popular in dynamic langauges?
I do not know what is popular, but implicit bool call are darn handy.

tjr

Sep 22 '08 #8
Steven D'Aprano:
>For many iterables, the amount of memory is not excessive and the increase in readability of len() is to be preferred over the side-effect of sum(1 for...).<
With side-effects do you mean the possibility of exhausting a lazy
iterable?

The readability difference is little, and it's way safer because it
works well enough with very long iterables too, so leniter(..) is
better than len(list(...)).

Bye,
bearophile
Sep 23 '08 #9
Carl Banks a écrit :
On Sep 22, 3:43 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
>bearophileH...@lycos.com a écrit :
>>Pekka Laukkanen:
but it still doesn't feel exactly right. Would it be worth submitting a bug?
It feels wrong because it is. In a tidier language (Pascal, Java, etc)
a boolean and an integer must be different types.
Some would argue (and some did by the time Python grew a 'bool' type)
that what is wrong is to have a bool type in a language that already
have a wider definition of the truth value of an expression...

And some would argue that it was wrong to have such a wide definition
for the truth value of an expression in the first place...
Indeed !-)
Sep 23 '08 #10
In message <ma**************************************@python.o rg>, Terry
Reedy wrote:
From this viewpoint, objecters would instead have to argue that it is
wrong to have such implicit calls and that programmers should have to
put them in explicitly.
But then again, you want to avoid unexpected restrictions like in Java,
where bool is a separate type, and while it is discrete, it cannot be used
to index arrays.

(Cf Pascal, where any discrete type could be used as an array index type.)
Sep 24 '08 #11

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

Similar topics

125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
1
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
5
by: Brian Richards | last post by:
I'm experiencing some wierd behavior with a Dictionary<T,U> class using foreach loops, such that the Key returned in the foreach is not contained in the dictionary. code: Dictionary<A, B>...
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
5
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}}...
0
by: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...

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.