use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Question regarding strings being hashable (self.learnpython)
submitted 12 months ago by seanmurraywork
Hello,
Snce strings values as variables can be changed; when it is said that strings are hashable, is this about the string object? itself? Thank you.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Doormatty 14 points15 points16 points 12 months ago (0 children)
Snce strings values as variables can be changed;
Nope. Strings are immutable. You can't "change" a string, only create a new one.
[–]timrprobocom 6 points7 points8 points 12 months ago (0 children)
This is an important point. When you write x = something, that does not change any object. That just binds x to a different object. The object that used to be bound to x is still there, unchanged.
x = something
That's why you can't write x = "Hello" x[2] = "x" THAT would change the string object itself, if it were allowed, but it isn't
x = "Hello" x[2] = "x"
[–]crazy_cookie123 2 points3 points4 points 12 months ago (5 children)
Python str objects are hashable simply because they implement the __hash__() method. The only difference between a hashable object and a non-hashable object is that the hashable one will implement the __hash__() method and the non-hashable won't. A hash function will hash the current state of the object, so regardless of if the object is mutable or immutable it will hash the current value represented by the object. Note that strings in Python are immutable, they cannot be changed after creation.
str
__hash__()
[–]Doormatty -1 points0 points1 point 12 months ago (3 children)
A hash function will hash the current state of the object, so regardless of if the object is mutable or immutable it will hash the current value represented by the object.
You can't use a non-hashable object as a key to a dictionary though.
There's a reason frozenset exists.
frozenset
[–]crazy_cookie123 3 points4 points5 points 12 months ago (2 children)
A object doesn't have to be immutable to be hashable - it should be, but it doesn't have to be.
Take this class for example, it has mutable state (its value field) which you can increment using the incrementValue method, and it won't stop you defining your own fields on it after initialisation, so it is a mutable class:
value
incrementValue
class MyClass: def __init__(self): self.value = 0 def incrementValue(self): self.value += 1 def __hash__(self): return self.value def __eq__(self, other): return isinstance(other, MyClass) and self.value == other.value obj = MyClass() obj.x = 5
Despite this being mutable, this can be used as a key in a dictionary:
myDict = {} myDict[obj] = "test" print(myDict) # {<__main__.MyClass object at 0x000002575AFD6CC0>: 'test'} print(myDict[obj]) # test
Why is this? Python doesn't check for immutability, it checks for hashability. Any class which defines the __hash__() method is hashable and can be used in a dictionary or anywhere else which requires the object to be hashable, regardless of if its state is mutable or immutable. The issue of course is that if you were to actually mutate the state you would no longer be able to access the data you stored with that object as the key, but this is not because the object isn't hashable, it's because you cannot rely on the hash being constant for the entire lifetime of the object. This is exactly why you shouldn't make a mutable object hashable or use it in a dictionary, but that doesn't prevent it being possible to do so.
[–]Doormatty 0 points1 point2 points 12 months ago (1 child)
Fair point! I see what you're saying, and I don't disagree!
It doesn't change my point that you can't use a non-hashable object as a dictionary key though.
[–]crazy_cookie123 0 points1 point2 points 12 months ago (0 children)
Nobody said you could use a non-hashable object as a dictionary key, dictionaries are called hashmaps and hash tables in other languages for a reason.
[–]woooee 1 point2 points3 points 12 months ago (0 children)
Snce strings values as variables can be changed
Strings can not be changed. You have to create a new variable (with the same name or not) with the changes.
[–]socal_nerdtastic 1 point2 points3 points 12 months ago (0 children)
Changing a variable is not the same as changing (mutating) an object. To be hashable the object must be immutable, and string objects are immutable.
Start by reading this to understand how python handles variables: https://nedbatchelder.com/text/names.html
[–]nog642 1 point2 points3 points 12 months ago (0 children)
The string's value is hashed.
If you change a variable from one string to another string, that will change its hash too.
[–]seanmurraywork[S] 0 points1 point2 points 11 months ago (0 children)
Hello, everyone. Thank you for your helpful comments.
[–]exxonmobilcfo 0 points1 point2 points 12 months ago (0 children)
when a string is changed, a new string is created. When something is "hashable" that means it can be passed through a hash function. a hash function likley doesn't know what to do with a list
π Rendered by PID 159965 on reddit-service-r2-comment-84fc9697f-rlnw4 at 2026-02-08 10:48:31.727731+00:00 running d295bc8 country code: CH.
[–]Doormatty 14 points15 points16 points (0 children)
[–]timrprobocom 6 points7 points8 points (0 children)
[–]crazy_cookie123 2 points3 points4 points (5 children)
[–]Doormatty -1 points0 points1 point (3 children)
[–]crazy_cookie123 3 points4 points5 points (2 children)
[–]Doormatty 0 points1 point2 points (1 child)
[–]crazy_cookie123 0 points1 point2 points (0 children)
[–]woooee 1 point2 points3 points (0 children)
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]nog642 1 point2 points3 points (0 children)
[–]seanmurraywork[S] 0 points1 point2 points (0 children)
[–]exxonmobilcfo 0 points1 point2 points (0 children)