BerkeleyJeff
asked on
Using setdefault to update that value of an int
I've recently been introducted to the setdefault function, and understand it can be used in the following way to update a dict's value, when that value is an array:
myDict.setdefault(myKey, []).append(myValue)
But how can I use setdefault to update the value of a dict, when that value is an int? For instance, the following statement gives me an error mesage:
myDict.setdefault(myKey, 0) += 1
Thanks!
myDict.setdefault(myKey, []).append(myValue)
But how can I use setdefault to update the value of a dict, when that value is an int? For instance, the following statement gives me an error mesage:
myDict.setdefault(myKey, 0) += 1
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, there's no mutable integer type. You could write one, but it wouldn't make any appreciable difference - rather than two dictionary accesses, you'd have one dictionary access plus an object attribute access (which is a dictionary access under the hood anyway).
ASKER
Thank you.
ASKER
Is there a mutable int type, like what "Integer" is to "int" in Java? Maybe I could halve the number of dict accesses that way.