[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Łukasz Langa

New submission from Łukasz Langa luk...@langa.pl:

Currently the constructor in defaultdict only accepts factories. It would be 
very handy to allow for concrete values as well. It's implementable either by 
checking if the argument is callable or by a new keyword argument.

--
assignee: lukasz.langa
components: Library (Lib)
keywords: easy
messages: 122382
nosy: holdenweb, lukasz.langa, michael.foord
priority: low
severity: normal
status: open
title: defaultdict constructor with a concrete value
versions: Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Michael Foord

Michael Foord mich...@voidspace.org.uk added the comment:

I would love this functionality (I almost always initialise defaultdict with a 
lambda that just returns a concrete value).

Unfortunately it seems like adding a keyword argument isn't possible because 
defaultdict takes arbitrary keyword args (and populates the dict with them).

defaultdict(lambda: 1, foo=1, bar=2, baz=3)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Eric Smith

Eric Smith e...@trueblade.com added the comment:

-1 from me. You can't use keywords, and if you make the value callable at a 
later date then suddenly you'll change the behavior of seemingly unrelated 
code. Is a lambda so bad?

--
nosy: +eric.smith

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Łukasz Langa

Łukasz Langa luk...@langa.pl added the comment:

Both arguments are true and definitive. Last possibility would be to introduce 
a factory function for defaultdicts that would only accept concrete values:

  from collections import fallbackdict

Then this factory could produce defaultdict instances like this:

  fallbackdict(0, some_key=1) -- defaultdict(lambda: 0, some_key=1)

That would be safe for future type changes as well.

If this version does not sound appealing, I can't see any other way of reliably 
introducing this shortcut.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Eric Smith

Eric Smith e...@trueblade.com added the comment:

How about:

from collections import defaultdict

class defaultdict_value(defaultdict):
def __init__(self, value):
defaultdict.__init__(self, lambda : value)

x = defaultdict_value(3)
print(x[1])

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Steve Holden

Steve Holden st...@holdenweb.com added the comment:

On 11/25/2010 11:48 AM, Eric Smith wrote:
 
 Eric Smith e...@trueblade.com added the comment:
 
 How about:
 
 from collections import defaultdict
 
 class defaultdict_value(defaultdict):
 def __init__(self, value):
 defaultdict.__init__(self, lambda : value)
 
 x = defaultdict_value(3)
 print(x[1])
 
 --

+1

But I'd call it defaultdict_const().

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Like three-liners?  whatsnew/2.5 gives us this one:

class zerodict(dict):
def __missing__(self, key):
return 0

I don’t think it’s too painful to have to use defaultdict with a lambda.  We 
can’t use a keyword argument and I’m -0.5 on changing behavior depending on the 
type of the first argument.

--
nosy: +eric.araujo

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Raymond Hettinger

Changes by Raymond Hettinger rhettin...@users.sourceforge.net:


--
assignee: lukasz.langa - rhettinger
nosy: +rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Raymond Hettinger

Raymond Hettinger rhettin...@users.sourceforge.net added the comment:

 It would be very handy to allow for concrete values as well.

Do you have use cases for a concrete integer value that isn't zero?

Since we can currently use defaultdict(int) or defaultdict(tuple), is the 
purpose just to create a more direct spelling of the same thing?  The docs for 
defaultdict also show a general purpose way to generate any default constant 
(though that way isn't obvious if you haven't seen it in the docs).

I'm reluctant to add yet another variant.  We already have __missing__, 
defaultdict, Counter, dict.get, and dict.setdefault().

The docs for dictionaries need to make clear that Guido has already provided an 
idiom to be the one obvious way to do it:

class MyConst(dict):
def __missing__(self, key):
return myconst

That was really the whole point of adding __missing__ in the first place.  The 
OP's proposal amounts to rejecting Guido's design which provides a very good 
general purpose solution.

--
keywords:  -easy
type:  - feature request

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Łukasz Langa

Łukasz Langa luk...@langa.pl added the comment:

A couple of points:

1. Eric's proposal is what I had in mind with the `fallbackdict' idea.
2. I'm also reluctant to add more variants to the standard library. Then again 
if it contained a `fallbackdict' I wouldn't probably ever use `defaultdict' 
again. How often do you need to provide a factory?
3. Naming the other variant `defaultdict_const', `defaultdict_value', 
`defaultdict_whatever' beats the purpose because it's actually more characters 
to type than `defaultdict(lambda:', especially when you count the longer import 
name.
4. I cannot come up with another typical integer value that would be useful, 
then again I've used , [] and set() numerous times. Adding zerodict, 
stringdict, listdict, setdict is obviously absurd.
5. The discussion started on Twitter amongst a couple of core devs and 
__missing__ didn't appear to be the one obvious way to anyone.
6. Of course I'm in no position to reject Guido's design on anything. Then 
again even `defaultdict(lambda:' is simply so much shorter than subclassing 
dict.

To sum up: if you don't find the idea of adding `fallbackdict' (possibly with 
an different *short* name) worth it, then I'm +1 on correcting the docs in 
terms of __missing__ and leaving the implementation as is.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Steve Holden

Steve Holden st...@holdenweb.com added the comment:

On 11/25/2010 1:44 PM, Łukasz Langa wrote:
 To sum up: if you don't find the idea of adding `fallbackdict'
 (possibly with an different *short* name) worth it, then I'm +1 on
 correcting the docs in terms of __missing__ and leaving the
 implementation as is.

+1

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10533] defaultdict constructor with a concrete value

2010-11-25 Thread Alex

Alex alex.gay...@gmail.com added the comment:

I agree with Łukasz, it's more clutter than is worth for what amounts to: 
fallbackdict = lambda c, **kwargs: defaultdict(lambda c, **kwargs)

I will note, however, that almost all my use cases are with factories, 
primarily list set or int, and it was only this week that I first needed 
something else!

--
nosy: +alex

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10533
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com