Hi all!
I am pretty sure this has been asked a couple of times, but I don't seem
to find it on the archives (Google seems to have a couple of problems
lately).
I am wondering what is the most pythonic way of dealing with missing
keys and default values.
According to my readings one can take the following approaches:
1/ check before (this has a specific name and acronym that I haven't
learnt yet by heart)
if not my_dict.has_key (key):
my_obj = myobject()
my_dict[key] = my_obj
else:
my_obj = my_dict[key]
2/ try and react on error (this has also a specific name, but...)
try:
my_obj = my_dict[key]
except AttributeError:
my_obj = myobject()
my_dict[key] = my_obj
3/ dict.get usage:
my_obj = my_dict.get(key , myobject())
I am wondering which one is the most recommended way? get usage seems
the clearest, but the only problem I see is that I think myobject() is
evaluated at call time, and so if the initialization is expensive you
will probably see surprises.
thanks in advance,
../alex
--
..w( the_mindstorm )p.
Jul 20 '07
25 4694
Alex Popescu a écrit :
Jakub Stolarski <ja************ *@gmail.comwrot e in
news:11******** **************@ k79g2000hse.goo glegroups.com:
>>Version 1 and 2 do different thing than version 3. The latter doesn't add value to dict.
As it was mentioned before, use: 1 - if you expect that there's no key in dict 2 - if you expect that there is key in dict
I may be missing something
You are.
but I think the 3 approaches are completely
equivalent in terms of functionality.
d = dict()
answer = d.get('answer', 42)
answer in d
=False
Carsten Haese <ca*****@uniqsy s.comwrote:
On Sat, 21 Jul 2007 09:22:32 +0530, Rustom Mody wrote
Can someone who knows about python internals throw some light on why
>>x in dic
is cheaper than
>>dic.has_key(x )
??
I won't claim to know Python internals, but compiling and disassembling the
expressions in question reveals the reason:
>from compiler import compile from dis import dis dis(compile("d ic.has_key(x)", "","eval"))
1 0 LOAD_NAME 0 (dic)
3 LOAD_ATTR 1 (has_key)
6 LOAD_NAME 2 (x)
9 CALL_FUNCTION 1
12 RETURN_VALUE
>dis(compile( "x in dic","","eval") )
1 0 LOAD_NAME 0 (x)
3 LOAD_NAME 1 (dic)
6 COMPARE_OP 6 (in)
9 RETURN_VALUE
"dic.has_key(x) " goes through an attribute lookup to find the function that
looks for the key. "x in dic" finds the function more directly.
Yup, it's mostly that, as microbenchmarki ng can confirm:
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' 'f(23)'
10000000 loops, best of 3: 0.146 usec per loop
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' '23 in d'
10000000 loops, best of 3: 0.142 usec per loop
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' 'f(23)'
10000000 loops, best of 3: 0.146 usec per loop
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' '23 in d'
10000000 loops, best of 3: 0.142 usec per loop
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' 'd.has_key(23)'
1000000 loops, best of 3: 0.278 usec per loop
brain:~ alex$ python -mtimeit -s'd={}; f=d.has_key' 'd.has_key(23)'
1000000 loops, best of 3: 0.275 usec per loop
the in operator still appears to have a tiny repeatable advantage (about
4 nanoseconds on my laptop) wrt even the hoisted method, but the
non-hoisted method, due to repeated lookup, is almost twice as slow
(over 100 nanoseconds penalty, on my laptop).
Alex
Bruno Desthuilliers <bd************ *****@free.quel quepart.frwrote in
news:46******** *************** @news.free.fr:
Alex Popescu a écrit :
>Jakub Stolarski <ja************ *@gmail.comwrot e in
>
[snip...]
d = dict()
answer = d.get('answer', 42)
answer in d
=False
Thanks. I think to make the 3rd approach completely equivalent I should
have been using d.setdefault(ke y, myojbect()).
../alex
--
..w( the_mindstorm )p.
"Rustom Mody" <ru*********@gm ail.comwrote:
Can someone who knows about python internals throw some light on why
>>>x in dic
is cheaper than
>>>dic.has_key( x)
??
Some special methods are optimised by having a reserved slot in the data
structure used to implement a class. The 'in' operator uses one of these
slots so it can bypass all the overheads of looking up an attribute such as
'has_key'.
On Jul 21, 7:48 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
"Rustom Mody" <rustompm...@gm ail.comwrote:
Can someone who knows about python internals throw some light on why
>>x in dic
is cheaper than
>>dic.has_key(x )
??
>From the 2.6 PEP #361 (looks like dict.has_key is deprecated)
Python 3.0 compatability: ['compatibility'-->someone should use a
spell-checker for 'official' releases]
- warnings were added for the following builtins which no
longer exist in 3.0:
apply, callable, coerce, dict.has_key, execfile, reduce,
reload
On Jul 19, 6:29 am, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
>
Myobject will be instanciated each time, yes.
and so if the initialization is expensive you
will probably see surprises.
No "surprise" here, but it can indeed be suboptimal if instanciating
myobject is costly.
What about this way ?
my_obj = my_dict.get(key ) or my_dict.setdefa ult(key,myobjec t())
Ciao
G.
On Sat, 21 Jul 2007 16:20:37 -0700, genro wrote:
On Jul 19, 6:29 am, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
>No "surprise" here, but it can indeed be suboptimal if instanciating myobject is costly.
What about this way ?
my_obj = my_dict.get(key ) or my_dict.setdefa ult(key,myobjec t())
Reduces the unnecessary instantiation of `myobject` to "false" objects.
May be not good enough.
Ciao,
Marc 'BlackJack' Rintsch
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrote:
>
Instead of doing:
if callable(functi on): function()
you should do:
try:
function()
except TypeError:
pass
That should work for most uses of callable(), but isn't quite the
same. (What if function() has side-effects, or is expensive, and you
want to determine if it is callable, but not actually call it _now_?)
The replacement for callable(x) is simply hasattr(x, '__call__').
Once upon a time it may have been that functions didn't have a __call__
attribute (I haven't checked back on really old Pythons top see if this
was the case), but these
>Also, what is the replacement of reduce? I think I remember seeing somewhere that lists comprehension would be (but also remember the advise that reduce will be quicker).
No, a list comprehension isn't equivalent to reduce(). There is no
replacement for reduce().
<snip>
There are of course several replacements for specialised uses of reduce:
sum, any, all. There is no general purpose replacement, but you can
write one in a few lines if you really need it.
>
It's a shame really. Oh well, maybe it will sneak back in via a
functional module, or itertools, or something. What a waste, what a
waste.
I'm sure it will reappear in some other module, but that's the correct
place for a little used function, not in builtins.
On Mon, 30 Jul 2007 07:37:05 +0000, Duncan Booth wrote:
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrote:
>> Instead of doing:
if callable(functi on): function()
you should do:
try: function() except TypeError: pass
That should work for most uses of callable(), but isn't quite the same. (What if function() has side-effects, or is expensive, and you want to determine if it is callable, but not actually call it _now_?)
The replacement for callable(x) is simply hasattr(x, '__call__').
Once upon a time it may have been that functions didn't have a __call__
attribute (I haven't checked back on really old Pythons top see if this
was the case), but these
Your sentence seems to have run out early :)
Thanks, I didn't know that -- I remember that Back In The Day checking if
something was callable was tricky, because it could be a function, a
method, or a class with __call__. It makes much more sense to make all
callables go through __call__.
>>Also, what is the replacement of reduce? I think I remember seeing somewhere that lists comprehension would be (but also remember the advise that reduce will be quicker).
No, a list comprehension isn't equivalent to reduce(). There is no replacement for reduce().
<snip>
There are of course several replacements for specialised uses of reduce:
sum, any, all. There is no general purpose replacement, but you can
write one in a few lines if you really need it.
True. But writing the same basic algorithm over and over again is about as
far from best practice as you can get without being a PHP programmer.
*wink* Currying is easy to do too, but Python has grown a function
functools.parti al() to do it properly. That's as it should be.
(Yes, I know that's a misuse of the term currying, but it's a common one,
and easier than saying "Making a partial function".)
>It's a shame really. Oh well, maybe it will sneak back in via a functional module, or itertools, or something. What a waste, what a waste.
I'm sure it will reappear in some other module, but that's the correct
place for a little used function, not in builtins.
Oh, I would have no problem at all with reduce being banished to the
functtools module. But it's a shame to have to go through the whole PEP
process, and risk Guido saying no.
--
Steven.
Steve Holden <st***@holdenwe b.comwrites:
[...]
Yup. Anyway there's a trivial translation for uses of apply.
apply(f, *args, **kw) = f(*args, **kw)
[...]
Steve means:
apply(f, args, kw) = f(*args, **kw)
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after them. What I want to do is filter out a subset of this
dict to produce another dict that satisfies a set of criteria (in this
case whether it contains all four values) to end up with something
|
by: David Rasmussen |
last post by:
If I have a collection of dicts like:
john = {'id': 1, 'name': "John Cleese", 'year': 1939}
graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941}
I could store all of them in a list. But for easy lookup, I might store
all these in a dict instead, like
people = {'1': john, '2': graham}
|
by: Gregory Piñero |
last post by:
Hey guys,
I thought I'd throw this out there since everyone loves optimization
questions (it's true, check out the number of replies to those type of
questions!)
Right now I have a function shown below. I want to combine two
dictionaries and add the values together where-ever there is overlap.
I figured this function would be reasonably fast, but I have around 1
million keys in each dictionary (~80% overlap) and it just runs all
|
by: David MacKay |
last post by:
Dear Greater Py,
<motivation note="reading this bit is optional">
I am writing a command-line reader for python.
I'm trying to write something with the same brevity
as perl's one-liner
eval "\$$1=\$2" while @ARGV && $ARGV=~ /^(\w+)=(.*)/ && shift;
|
by: aking |
last post by:
Dear Python people,
im a newbie to python and here...so hello!
Im trying to iterate through values in a dictionary so i can find the
closest value and then extract the key for that value....what ive done so far:
def pcloop(dictionary, exvalue):
z = dictionary.itervalues()
y = z - exvalue
| |
by: Andy Dingley |
last post by:
I'm trying to write rot13, but to do it in a better and more Pythonic
style than I'm currrently using. What would you reckon to the
following pretty ugly thing? How would you improve it? In
particular, I don't like the way a three-way selection is done by
nesting two binary selections. Also I dislike stating the same
algorithm twice, but can't see how to parameterise them neatly.
Yes, I know of .encode() and .translate().
No, I...
|
by: james_027 |
last post by:
hi,
a_dict = {'name':'apple', 'color':'red', 'texture':'smooth',
'shape':'sphere'}
is there any difference between ..
for key in a_dict:
from
|
by: Seongsu Lee |
last post by:
Hi,
I have a dictionary with million keys. Each value in the
dictionary has a list with up to thousand integers.
Follow is a simple example with 5 keys.
dict = {1: ,
2: ,
900000: ,
900001: ,
|
by: Florian Brucker |
last post by:
Hi everybody!
Given a dictionary, I want to create a clustered version of it,
collecting keys that have the same value:
{1:, 2:, 3:}
That is, generate a new dict which holds for each value of the old dict
a list of the keys of the old dict that have that very value.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |