473,221 Members | 2,041 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,221 software developers and data experts.

Should: "for k,v in **dictionary_instance" work?

Does it make sense to provide this syntax for iterating key/value
pairs from a dictionary?

for k,v in **dict():
print k,v

why is this not the same as:

for k,v in dict().items():
print k,v

for that matter, why the heck doesn't a dictionary default to
returning a tuple
k,v pair from its iterator?

Pax, Keith

Ps, I'm sure someone has thought of these things before, probably been
answered before, but I'm sure I didn't find reference to them when i
searched.

Jun 15 '07 #1
5 5688
In <11**********************@n2g2000hse.googlegroups. com>, keithgabryelski
wrote:
Does it make sense to provide this syntax for iterating key/value
pairs from a dictionary?

for k,v in **dict():
print k,v

why is this not the same as:

for k,v in dict().items():
print k,v
Why should it be? Why adding something that unreadable and magic instead
of the perfect readable ``for k, v in some_dict.iteritems():``?

And I don't see why it should be ``**``!?

Ciao,
Marc 'BlackJack' Rintsch
Jun 15 '07 #2
On Jun 15, 2:17 pm, keithgabryel...@gmail.com wrote:
Does it make sense to provide this syntax for iterating key/value
pairs from a dictionary?

for k,v in **dict():
print k,v

why is this not the same as:

for k,v in dict().items():
print k,v

for that matter, why the heck doesn't a dictionary default to
returning a tuple
k,v pair from its iterator?

Pax, Keith

Ps, I'm sure someone has thought of these things before, probably been
answered before, but I'm sure I didn't find reference to them when i
searched.
The answer is simply for history and for clarity.

Jun 15 '07 #3
On Fri, 15 Jun 2007 19:17:25 +0000, keithgabryelski wrote:
Does it make sense to provide this syntax for iterating key/value
pairs from a dictionary?

for k,v in **dict():
print k,v

why is this not the same as:

for k,v in dict().items():
print k,v
Because *t and **d already have well-defined meanings, and that's not
what **d would mean.

In a function definition, *t collects positional arguments into a tuple
and **d collects keyword arguments into a dictionary. In a function call,
they expand them again. For example:

function(1, 2, 3, a=4, b=5, c=6)
==t = (1, 2, 3); d = {'a':4, 'b':5, 'c':6}

function(*t, **d)
==function(1, 2, 3, a=4, b=5, c=6)
Currently, *t and **d are syntax errors outside of function calls and
definitions. (Any other places?) But if they were allowed, what would they
mean? For consistency, we would expect the following:

t = tuple(1, 2, 3)
for item in *t:
# same as "for item in 1, 2, 3:"
print item
Even if it were allowed, it would be pointless: it would just expand the
tuple only to collect it again.

What would **d mean?

d = dict(a=1, b=2, c=3)
for key, value in **d:
# same as "for key, value in 'a': 1, 'b': 2, 'c': 3:"
print key, value
For starters, that would mean changing the syntax of Python to allow
key:value assignments outside of a dict constructor. I can't think of a
reason to do so, but even if there is, we have the same problem as for the
tuple expansion: it would pointlessly expand the dict, only to collect it
again.

Maybe you don't care about consistency and you just want the compiler to
accept "for k,v in **d" as syntactic sugar for "for k,v in d.items()". Or
perhaps d.iteritems() would be a better choice.

In that case, Python isn't very big on syntactic sugar merely for saving a
few characters of typing, so you're unlikely to convince those who would
need convincing.

for that matter, why the heck doesn't a dictionary default to
returning a tuple
k,v pair from its iterator?
When dictionaries were first made iterable, in Python 2.2 if I recall
correctly, there was debate about whether "for thing in dict:" should be
equivalent to iterating over the keys, the values or both. In particular,
people pointed out that there were just as many, or slightly more,
examples of "for k,v in dict.items()" as "for k in dict.keys()" in the
standard library. So there is a good case for making iterating over a
dictionary equivalent to iterating over the keys and values.

But the deciding point was the correspondence between "for x in dict" and
"if x in dict". In fact, Python then, and now, included a fall-back
iterator protocol: if __getitem__ is defined, iteration over an object is
equivalent to repeatedly calling __getitem__.

You can read the PEP that covers some of these issues here:

http://svn.python.org/projects/peps/trunk/pep-0234.txt

--
Steven.

Jun 16 '07 #4
On Jun 16, 5:27 am, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
Currently, *t and **d are syntax errors outside of function calls and
definitions. (Any other places?) But if they were allowed, what would they
mean?
Actually since you asked, I had to try this out

x = range(10)
a, *b = x

I would take it to mean a = x[0], b = x[1:] (or more precicely, it
would
probaby have to use the iter protocol). Unfortunately it fails..
Since these are roughly equivalent:

t = 1, (2, 3)

a, (b, c) = t

def foo(a, (b, c)): print a,b,c
foo(*t)

I would like to see the assignment enhanced so that this works too

t = 1, (2, 3), 4, 5

a, (b, c), *d = t

def foo(a, (b, c), *d): print a,b,c,d
foo(*t)

The latter one with function definition works fine, the former
assignment doesn't (SyntaxError). It's a bit surprising. Is there
any reason not to support this syntax? It would be neatly symmetric
in my opinion, so one wouldn't have to learn anything new. In fact,
this addition would reduce the amount of things one must learn
(unexpected behaviour).

def rawr((a, b, (c, *d)), (e, *f), **g): pass

Jun 16 '07 #5
Actually since you asked, I had to try this out
>
x = range(10)
a, *b = x
PEP 3132: Extended Iterable Unpacking
http://www.python.org/dev/peps/pep-3132/

--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Jun 16 '07 #6

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

Similar topics

4
by: Phil Powell | last post by:
re: http://us4.php.net/manual/en/function.imagegif.php I have one class method that resizes images, including GIF images, and works just fine doing so, even up to this line: eval('image' ....
23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
7
by: Petr Prikryl | last post by:
Hi, Summary: In my opinion, the C-like prefix increment and decrement operators (++i and --i) should be marked as "syntax error". Current situation: try... (Python 2.4 (#60, ...)) >>> i =...
1
by: wireless200 | last post by:
I'm running EM on my local box and sql server on a remote internet accessed box. How do I specify a file path for a DTS package to access files on the remote box? For example, to run a local...
10
by: Terry Olson | last post by:
I want to search a textbox and replace any instances of the dollar sign character. But when I do that it thinks it won't work because it think's I'm looking to replace something at the end of the...
1
by: Jongmin Lee | last post by:
Hi everybody, I overloaded "operattor== " for comparing two objects. However, I can't compare one instance to null valued object instance. How to solve this problem? public class Custom {...
12
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL...
5
by: SharpCoderMP | last post by:
is it possible to implement some safe way of performing two or more instructions in a kind of "atomic" way? consider this: we have two queues (implemented on top of an List<T>). now we need to...
13
by: Wayne | last post by:
I have just experienced my first late paying client. By "late" I mean that the invoice is now almost 2 months overdue. I don't think that the client is maliciously withholding payment - rather...
1
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.