-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add (get ...) with default value #34
Comments
My suggestion on IRC is to move |
I like this suggestion. PR coming up in a bit. |
There's a slight problem here: Hy's |
So, unless someone knows a way how to do this in a sane way, I'd say |
Based on the problem explained above, I'm closing this, because there's no way to properly implement it. |
A bit late to the party, but I too would like to see this feature in the standard library.
So you're implying that if a user expects
If we rely on exceptions, then yes, we have a problem with "whatnot", since we don't know what could it raise.
Well, there are other options. Instead of relying on exceptions, we could check keys of a dict or length of a list. There's still a problem with "whatever", but at least we won't need to catch all exceptions. Instead of making a truly generic get-with-default, maybe it will be enough to only support dict- and list-like collections? Then, if a container raises another exception, we will not catch it. Or catch it and raise another exception, stating that container is not supported. Or, if we don't rely on exceptions, but can't check the length or the keys of a container, also raise an "unsupported" exception. Instead of putting this feature inside the |
I would also like to point out that Clojure's get, which OP mentioned, doesn't rely on exceptions, but checks map's keys and length of an array or a string. And for unsupported/null collections it simply returns the default value. |
Okay, I think those could be reasonable compromises. |
I'm not too worried about that at the moment. We've been breaking API every release, and have more breaking changes planned. That said, Hy's
Exactly how would you compile that? Catching the exception might actually be easier. Both |
Thinking about this some more, we could probably write a macro to do this, and avoid catching extra exceptions with gensyms. (defmacro/g! get [coll key &optional default]
`(do
;; eval coll & key outside try
(setv ~g!coll ~coll
~g!key ~key)
(try
(. ~g!coll[~g!key])
(except [LookupError]
~default)))) But this doesn't really seem better than a simple function. The only advantage is that it doesn't have to evaluate the def get(coll, key, default=None):
try:
return coll[key]
except LookupError:
return default But by then why not use |
Coming from Clojure, I was expecting this to work:
However, instead I get KeyErrors. I have come to find out that
(get ...)
is closer to Clojure's(get-in ...)
.It would be great if there was a "get-with-default" equivalent, otherwise I have to fall back to Python's implementation of
get
:The text was updated successfully, but these errors were encountered: