Steve,
I take your point, and like indicated in another part of this thread, for most parts of our stack its standard practices. It is only when managing certain external resources that we take a bit of extra care, and that is in a library that is not outwardly exposed.

I agree with you also that I don't even know if this is a thing that is possible in python unless the type checker could assume that a caller was meaning to move a variable when they called a function declared with move. And of course this would still be at the type checking level, and not at execution time.

For our use case, we put a move indicator at the point the function is called, and not in the signature of the function itself (because of what you indicated above), signaling the intention was to move it. It's not really much different than f(x); del x (though of course there are details). And so python does provide everything needed already to ensure one logical binding (and of course there are N other solutions people can think of, guards, exceptions, etc).

My point was more that if there was a way I could indicate to a type checker that it may be unsafe for anything to use a reference to this object after this function call it would be helpful(as I might have forgotten behavior of functions, or simply not been paying attention).

On Thu, Nov 26, 2020 at 3:53 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Nov 26, 2020 at 02:15:50PM -0500, nate lust wrote:

> It would be convenient if you could mark in source code that you intended a
> resource to be "moved" and any further access through other bindings are
> not what was intended. This would help catch logic bugs during type
> checking, instead of hitting this issue at runtime.

I can't help but feel that if your code relies on the concept of
"moving" an object, your concept is already at odds with Python's object
and execution model.

But putting that aside, is this is even technically possible? Can type
checkers track the "owner" of a resource in this way?


    obj = {}      # obj is owned by this code block
    process(obj)  # still owned by this block
    print(obj)    # so this is okay
    capture(obj)  # but now ownership is stolen by capture()
    print(obj)    # and this is a type-error (?!)


Maybe I'm just not familiar enough with the type-checking state of art,
but I would be amazed if this was possible.

By the way, I have intentionally used the term "stolen" rather than
"taken" or "transfered to" with respect to moving ownership of `obj`. At
the caller's site, there is no obvious difference between the calls to
process() which doesn't move obj, and capture() which does.

Any innocuous-looking function could "move" ownership, with *or without*
the knowledge of the caller. It's true that the Python interpreter
itself has no concept of "ownership" in this sense, but to the degree
that the coder cares about such ownership, having it moved without their
knowledge and consent is surely "theft" :-)


--
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MNWGVXD2HSG7JY673PWX63VCFVPCXDZW/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Nate Lust, PhD.
Astrophysics Dept.
Princeton University