Posts: 4,647
Threads: 1,494
Joined: Sep 2016
dict instances have a .get() method that is similar to the .pop() they also have, except nothing is removed and the key is not in the dict instance, it returns None.
why do we need to have this? is it so we can have shorter/smaller code, since this can be done without it?
if so, then why not have a .put() method that has an optional 3nd argument for an alternate value to put in under the key in the 1st argument if the key will be new (does not already exist) in that dict instance?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(May-07-2023, 02:53 AM)buran Wrote: dict.setdefault
is that new in 3.11? it is not there in my 3.8.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,790
Threads: 76
Joined: Jan 2018
May-11-2023, 05:18 AM
(This post was last modified: May-11-2023, 05:18 AM by Gribouillis.)
(May-11-2023, 02:58 AM)Skaperen Wrote: is that new in 3.11? it is not there in my 3.8. No, dict.setdefault is new in Python 2.0 (October 16, 2000).
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-11-2023, 05:37 AM
(This post was last modified: May-11-2023, 05:38 AM by buran.)
Here it is in python 3.8.
As @ Gribouillis said it was in python2 as well.
Here is link to a nice presentation Code Like a Pythonista: Idiomatic Python delivered at PyCon2006 by David Goodger ( setdefault(1), setdefault(2)). Almost all of the presentation very much still valid today, with some parts like Advanced % String Formatting having more modern alternatives like f-strings
So yeah, it is there for some good 20+ years
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
then i had to have mis-typed the name. i see it in dir({}) . but this method is not doing what i expect it to do. the doc is vague and my expectation is had before i read this doc so that may be why it is vague to me and why i still expect a different behavior. i don't understand why it needs a key.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,790
Threads: 76
Joined: Jan 2018
May-12-2023, 06:40 AM
(This post was last modified: May-12-2023, 06:43 AM by Gribouillis.)
(May-12-2023, 12:04 AM)Skaperen Wrote: this method is not doing what i expect it to do. the doc is vague and my expectation is had before i read this doc so that may be why it is vague to me and why i still expect a different behavior. setdefault() is just like get() but it inserts the default value in the dict if the key is missing.
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-12-2023, 08:05 AM
(This post was last modified: May-12-2023, 08:05 AM by buran.)
(May-12-2023, 12:04 AM)Skaperen Wrote: i don't understand why it needs a key. Without supplying a key, how would you know where (what key) to assign the [default] value?
spam = {} # empty dict
foo = spam.setdefault('eggs', 'foo') # of course you don't HAVE TO assign the return value to a name
print(spam)
print(foo)
bar = spam.setdefault('eggs', 'bar')
print(spam)
print(bar) Output: {'eggs': 'foo'}
foo
{'eggs': 'foo'}
foo
(May-06-2023, 11:55 PM)Skaperen Wrote: why not have a .put() method that has an optional 3nd argument for an alternate value to put in under the key in the 1st argument if the key will be new (does not already exist) in that dict instance?
It's doing exactly that, except that I don't understand what would be the purpose of the second argument of your dict.put() - i.e. it doesn't make sense to have 3 arguments for what you describe
Posts: 4,790
Threads: 76
Joined: Jan 2018
(May-12-2023, 08:05 AM)buran Wrote: I don't understand what would be the purpose of the second argument of your dict.put() - i.e. it doesn't make sense to have 3 arguments for what you describe As far as I understand the description, it is
def put(dic, k, v, w=None):
dic[k] = (v if k in dic else w) Use cases are hard to find.
Posts: 8,160
Threads: 160
Joined: Sep 2016
(May-12-2023, 08:36 AM)Gribouillis Wrote: As far as I understand the description, it is Basically one value if key already in dict and another value if key not yet in dict?
(May-12-2023, 08:36 AM)Gribouillis Wrote: Use cases are hard to find. Agree
|