Advanced Dictionary

T

Thomas Lehmann

Hi,

I have seen a recipe which allows auto creation of missing values for
dictionaries.
However this recipe is not working for all.

class AutoValueDict(dict):
def __makeitem__(self, key):
return self.setdefault(key, {})

def __getitem__(self, key):
return self.get(key, self.__makeitem__(key))

I would like to have a dictionary which ensures dictionaries as values
except when I'm assigning another:

dict["abc"]["xyz"]["123"]["456"] = 123

How can I do this without many "if" and "else"?

best regards
Thomas
 
T

Thomas Lehmann

class AutoValueDict(dict):
    def __makeitem__(self, key):
        return self.setdefault(key, {})

    def __getitem__(self, key):
        return self.get(key, self.__makeitem__(key))

I would like to have a dictionary which ensures dictionaries as values
except when I'm assigning another:

dict["abc"]["xyz"]["123"]["456"] = 123

How can I do this without many "if" and "else"?

Oh no... of course like this:
return self.setdefault(key, AutoValueDict())
 
P

Peter Otten

I think it's bad style to invent your own __whatever__() methods, I'd rather
call them _whatever().
def __getitem__(self, key):
return self.get(key, self.__makeitem__(key))

I would like to have a dictionary which ensures dictionaries as values
except when I'm assigning another:

dict["abc"]["xyz"]["123"]["456"] = 123

How can I do this without many "if" and "else"?

Oh no... of course like this:
return self.setdefault(key, AutoValueDict())

An alternative implementation (requires Python 2.5):
.... def __missing__(self, key):
.... print "creating subdict for", key
.... value = A()
.... self[key] = value
.... return value
....
creating subdict for x
creating subdict for y{'x': {'y': {'z': 42}}}

Peter
 
I

Ian Kelly

Hi,

I have seen a recipe which allows auto creation of missing values for
dictionaries.
However this recipe is not working for all.

class AutoValueDict(dict):
   def __makeitem__(self, key):
       return self.setdefault(key, {})

   def __getitem__(self, key):
       return self.get(key, self.__makeitem__(key))

I would like to have a dictionary which ensures dictionaries as values
except when I'm assigning another:

dict["abc"]["xyz"]["123"]["456"] = 123

How can I do this without many "if" and "else"?

Why not use defaultdict?

from collections import defaultdict

def recursive_defaultdict():
return defaultdict(recursive_defaultdict)

my_dict = recursive_defaultdict()
my_dict["abc"]["xyz"]["123"]["456"] = 123

Cheers,
Ian
 
S

Stephen Hansen

I think it's bad style to invent your own __whatever__() methods, I'd rather
call them _whatever().

It goes a bit beyond bad style into, "it is explicitly forbidden", I think.

Not that "forbidden" means, "Python will prevent you from trying", just
that it says, "Don't do it."

Leading-and-trailing double underscores are explicitly reserved for
Python to define as Special. They also imply a slightly different
calling semantic: normal leading-and-trailing double underscore methods
bypass instance lookup.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMGPkvAAoJEKcbwptVWx/lEy8H/2vmFntgOgfLUulBr/AbD1zc
TBgFhrDRa7iqtB0BkGN4OrbTNlLtVy5hFyDkd0qgRBrYRYEJB67kCj0yrtmst3dy
diVuQhylXGRNznNr60UETDpHl1eEA6XMkVj2Ou/uvdvzBPbGCw4sVvZJ1MyfeLXE
LkWaMMvIcHJN5WzzGaEUws65AjXBXubrIecb+ahQ2Bx+KSqEFziP+hhV+cSMDH9X
YIp/iKnhVccgZoVsGD3rvo4zPAMSNek3IEnE4gslLfdj/60CRuvhZnuRuIiBVifI
IDcUri3B9XlotIP1otdNUaLWu9aYW7Vk/F2xIE1BoM5awMSLr0NGTmqry3kyEA0=
=ZGp5
-----END PGP SIGNATURE-----
 
S

Steven D'Aprano

Leading-and-trailing double underscores are explicitly reserved for
Python to define as Special.


That part is correct. But of course Python doesn't prevent you from
ignoring this rule (more of a guideline really).


They also imply a slightly different
calling semantic: normal leading-and-trailing double underscore methods
bypass instance lookup.

This is semi-correct. Operator overloading and other special method
lookup bypasses instance lookup and calls type(instance).__method__ but
that has nothing to do with the double-underscore methods themselves.
They operate normally if you call them by hand:

.... def __add__(self, other):
.... return "special"
....'not very special'
 
S

Stephen Hansen

That part is correct. But of course Python doesn't prevent you from
ignoring this rule (more of a guideline really).

Agreed. Its a namespace reserved for Python itself to define. But
"reserved" in the more traditional Python sense of it saying, "Please
keep your hands off", and not, "SyntaxError: Illegal creation of new
special method".
This is semi-correct. Operator overloading and other special method
lookup bypasses instance lookup and calls type(instance).__method__ but
that has nothing to do with the double-underscore methods themselves.
They operate normally if you call them by hand:

Well, yes; I was simplifying slightly because people don't really call
them by hand in the normal course of events (and eventually you'll pass
the object into some internal where it most certainly doesn't get called
that way).

But true, that's more accurate. :)

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMGQCYAAoJEKcbwptVWx/l5EIH/0bahUXbsxz8JKi6WulcXtXz
qcJtcYyNrKk2tBwubd3aTLxjVYbUNna1nkuV2D3EZ48/4cEKJ7wc3Hhz7s1YXeYw
B/5qjeoVg8jBfTQg9Y4Vlb3ZhuBGWIWyN7QYLf7qdNP6kSi6dZCVpHV8SoillrcK
Y2+rbZ3UAQxDfgWWsVOSesQkKwGOZ10e79snBQw4lFIqlLKHEcfV3fIz4Z5meGtx
mCZtOdCotW2ktnxMWgDQuD0NNKYMsEGb9P2vL6DjwCVTBSvpXt1Uw2g3Hu1E7nQH
LcVRy1yduFcElryTyQmMf4i4KmDK0wNBvoIKaLEcbMs2jBY7v2YCox8vxcT7aW0=
=qpXs
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top