-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
keyword lookup on NamedTuple #202
Comments
Clojure Keywords are not currently polymorphic with python __getattr__.
…On Fri, Apr 22, 2022 at 1:23 AM shaun ***@***.***> wrote:
I believe a NamedTuple
<https://docs.python.org/3.3/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields>
in python supports two types of lookups: attribute name or index.
Right now, I have to use (py.- nt :foo) but (:foo nt) returns nil. I
believe this is because the jvm bridge is not able to treat a namedtuple as
both a dict and a tuple. Is this possible? Thanks!
—
Reply to this email directly, view it on GitHub
<#202>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACPJX42LLX3M4TWEMMF45ILVGIZWZANCNFSM5UBF5LRA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Did you require “py.”?
…On Fri, Apr 22, 2022 at 1:35 AM shaun ***@***.***> wrote:
Also, while trying to convert a namedtuple to a dict using ._asdict()
<https://docs.python.org/3/library/collections.html#collections.somenamedtuple._asdict>,
I had a peculiar run-in with the following two expressions which I presumed
would be equivalent:
- (py. nt _asdict) fails with "unable to resolve classname: py"
- ((py.- nt _asdict)) succeeds
—
Reply to this email directly, view it on GitHub
<#202 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACPJX43NDCFWRAR4HMB7AHLVGI3BPANCNFSM5UBF5LRA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Thanks. Is there a way to make keywords polymorphic to getattr from the user side? |
(moved to comment below b/c apparently markdown doesn't work from email)
|
Do you mean extending libpython-clj itself? We do have hooks into the You could probably do something like: (defmethod pydafy 'builtins.NamedTuple [nt]
;; code to convert named tuple -> clojure, possibly in conjunction
;; with a Clojure NamedTuple protocol that extends `get`
... )
note that once you've extended with (:foo (datafy nt))
There are lower level hooks available for performant marshalling of |
Thanks so much for the example, I was not familiar with And just to share here, I missed the following distinction in Python (since they’re both equivalent in JS):
So I understand your original response now, that keywords map to getitem, whereas |
Great point! We're always discussing the right balance to strike in how
much to hide/expose. Always open to suggestions, thank you for your input!
…On Fri, Apr 22, 2022 at 1:15 PM shaun ***@***.***> wrote:
Thanks so much for the example, I was not familiar with datafy. Glad to
know this.
And just to share here, I missed the following distinction in Python
(since they’re both equivalent in JS):
- __getitem__ is for foo['bar'] = foo.get('bar')
- __getattr__ is for foo.bar = getattr(foo, 'bar')
So I understand your original response now, that keywords map to getitem,
whereas py.- is to be used for getattr. I’ll just use it that way to not
hide what's happening in python. Thank you!
—
Reply to this email directly, view it on GitHub
<#202 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACPJX44SHSJV2VVHVJTJSQLVGLNCHANCNFSM5UBF5LRA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Thanks. I don't really know what I'm doing yet, but I can't believe this project exists AND works flawlessly so far. Thanks for all your work 🙏 |
I believe a NamedTuple in python supports two types of lookups: attribute name or index.
Right now, I have to use
(py.- nt :foo)
whereas(:foo nt)
does not work and returns nil. I believe this is because the jvm bridge is not able to treat a namedtuple as both a dict and a tuple. Is this possible?Thank you!
(I deleted a followup comment about
(py. nt _asdict)
not working, but it was my error in not requiring "py.")The text was updated successfully, but these errors were encountered: