You sho-bout that?
If you're new to programming, you can learn a lot more by listening than by arguing.
You sho-bout that?
If you're new to programming, you can learn a lot more by listening than by arguing.
>>> {}.get({}) is None
TypeError: unhashable type: 'dict'
>>> {}.get("key") is None
True
>>> {}.__hash__ is None
True
>>> "key".__hash__ is None
False
Let me fix your example for you: def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
Although really, the C implementation does this, indirectly: def get(self, key, default=None):
return self[key] if key in self else default
The reason I say indirectly is because (a) there's no exception handling in use in the C case and (b) there's no equivalent to the hash table lookup failing when written in Python, so it's an inexpressible concept. That Python will search twice, while the C does not. The KeyError except is a nice analog, but CPython does not futz with stack frames at all if the key lookup fails, so it's not directly comparable to your version. My final version is the closest you'll get to translating what Python does to Python.If you're speaking with someone new, you can learn a lot more by not condescendingly assuming competence of the other party, particularly when they correctly spotted those three problems and you didn't. I trust this reply will alleviate the misconception.
[0]: https://github.com/python/cpython/blob/master/Objects/dictob...
[1]: https://github.com/python/cpython/blob/master/Objects/dictob...
I don't know what typo my first example has (the bug you mentioned), but the point is to teach you about the value of using try/except in abstraction, rather than littered throughout your code.
Despite our alpha mentalities, we are not worlds apart, truthfully. My original response came across sounding hardliner pro-try-except, anti-anything-else, but my point was only that people abuse exception handling in their primary business code (e.g., directly handling a network IOError in a Django view). As a result, they have a bad time. Libraries and utils should be relegated to exception handling duties like that. When following principles like developers love Python's try / except blocks for their clarity.
Do not project your "alpha mentality" onto me, please. I am the polar opposite of "alpha." That was all you.
You were wrong, and I didn't stoop down to assuming anything about your competence as you so desperately tried to get me to do. Instead of blaming wine like a child, you should probably objectively look at yourself and your judgments of people. Your attitude is not uncommon, and I specifically look for it when I interview.
I'd have a lot more respect for you if you'd own the mistake, but I can tell that you're not going to, so this will be my last comment in this thread as well.