Skip to content
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

Dunder methods #43

Closed
erhlee-bird opened this issue Sep 14, 2018 · 7 comments
Closed

Dunder methods #43

erhlee-bird opened this issue Sep 14, 2018 · 7 comments

Comments

@erhlee-bird
Copy link
Contributor

The magic/dunder methods rely on identifiers with underscores incompatible with Nim identifiers.
Below is an example of a common interaction with Python context managers where a slight workaround is needed to invoke such methods.

import macros

import nimpy

let None = pyBuiltins().None

template with_py(context: typed, name: untyped, body: untyped) =
  try:
    # Can't use the `.` template
    let name = callMethod(context, "__enter__")  # context.__enter__()
    body
  finally:
    discard callMethod(context, "__exit__", None, None, None)

let os = pyImport("os")

os.scandir(os.getcwd()).with_py(context):
  for entry in context:
    echo entry.name.to(string)

This works just fine, but it seems like this could be a spot for some convenience wrapper/convention for dealing with Python context managers. Any thoughts on anything nimpy might want to support?

@alehander92
Copy link

I am surprised b.__enter__ doesn't work, maybe that would make sense to be supported by nim's quotes

@Vindaar
Copy link
Contributor

Vindaar commented Sep 17, 2018

So patching the lexer to allow underscored identifiers within accent quotes is pretty straight forward. See: Vindaar/Nim@d047887
Makes your example work fine, if as @alehander42 suggested, __enter__ is put into backticks.

If promoting good Python interoperability is a goal of Nim, this would be nice to have I guess. I can see it opening a can of worms though, although all tests seem to pass on my machine.
I'm pretty skeptical this would actually get merged though. :)

@yglukhov
Copy link
Owner

I'm pretty skeptical this would actually get merged though

There's only one way to find out :)

@alehander92
Copy link

Please, PR it to Nim @Vindaar, I think it is useful

@yglukhov
Copy link
Owner

Ok, so the alternatives are:

  1. Keep the status quo. Since dunder methods are not used often, it's ok for them to look not pretty.
  2. Provide pretty aliases, with nim-friendlier names.

I'd go with 1 tbh, but I admit I'm not a day-to-day python user, so I might be missing smth.

@yglukhov
Copy link
Owner

yglukhov commented Nov 7, 2018

Closing this since no objections. Feel free to bring more arguments though.

@yglukhov yglukhov closed this as completed Nov 7, 2018
@xyb
Copy link
Contributor

xyb commented Aug 16, 2021

@yglukhov Hopefully we can reconsider this, because we can't define iter and next to implement a Python iterator without supporting the names of the magic methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants