Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for Python dictionary with default key

Current License: CC BY-SA 3.0

20 events
when toggle format what by license comment
Jan 19, 2022 at 0:06 answer added Friedrich -- Слава Україні timeline score: 1
Oct 29, 2015 at 1:39 vote accept gt6989b
Oct 29, 2015 at 1:35 answer added martineau timeline score: 1
Oct 28, 2015 at 23:08 comment added gt6989b @jonrsharpe i did not mean to write it in code -- just copy/paste your comments in an answer so i can accept it and give you credit your advice deserves
Oct 28, 2015 at 23:06 comment added jonrsharpe @gt6989b no, I'm not writing it for you (or, rather, not posting what I wrote to check you could do it easily). Go and give this a go yourself - that's how you learn! If you get stuck, then you can ask a decent question with a minimal reproducible example and specific error message.
Oct 28, 2015 at 23:06 review Close votes
Nov 1, 2015 at 0:05
Oct 28, 2015 at 23:06 comment added gt6989b @jonrsharpe thanks, feel free to write out your suggestion as an answer and i will be happy to accept it
Oct 28, 2015 at 23:04 comment added jonrsharpe @gt6989b if it makes you feel better: as far as I'm aware there isn't a class in the standard library that implements what you want out of the box, so you will have to write this yourself. However, it's not difficult to do so, either subclassing e.g. dict or starting from collections.MutableMapping.
Oct 28, 2015 at 23:03 comment added gt6989b @jonrsharpe i was hoping there would be an already implemented class to do something like that, it seems like a very standard task to do -- and figured i am not googling something right
Oct 28, 2015 at 23:01 comment added jonrsharpe @DeliriousMistakes it really isn't!
Oct 28, 2015 at 23:01 comment added user5416120 @jonrsharpe That's a lot of lines :P
Oct 28, 2015 at 23:00 comment added jonrsharpe You can do it in under 20 lines, including the import, it's really not that big of a deal! If you want to try inheriting an existing class and implementing __getitem__, then try doing that; it's still not clear why you're asking the question.
Oct 28, 2015 at 22:58 comment added gt6989b @jonrsharpe i was hoping i could get something easier by inheriting from a ready-made object, like defaultdict, and just overriding __getitem__, leaving same functionality everywhere else...
Oct 28, 2015 at 22:54 comment added jonrsharpe You inherit them, then implement the methods they specify (in this case __[get/set/del]item__, __iter__ and __len__, plus your custom __init__ for the default key-value pair).
Oct 28, 2015 at 22:54 comment added gt6989b @jonrsharpe yes, defaultKey is param at initialization, that the instance should store as an attribute
Oct 28, 2015 at 22:52 comment added gt6989b @jonrsharpe yes, that's my question, how to implement this. I am not sure how to use the ABCs in this context
Oct 28, 2015 at 22:52 history edited jonrsharpe CC BY-SA 3.0
deleted 23 characters in body
Oct 28, 2015 at 22:50 answer added Yash Mehrotra timeline score: 5
Oct 28, 2015 at 22:50 comment added jonrsharpe What's your question, exactly? You're right that dict.get won't do that; if you want to know how you could write your own, look at docs.python.org/2/library/… and implement __getitem__ accordingly. But where should defaultKey come from (is it a parameter? An attribute of the class, or of the instance?)
Oct 28, 2015 at 22:48 history asked gt6989b CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /