osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

About the implementation of del in Python 3


On Fri, Jul 7, 2017 at 1:21 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Steve D'Aprano <steve+python at pearwood.info>:
>
>> On Thu, 6 Jul 2017 07:24 pm, Marko Rauhamaa wrote:
>>
>>> While talking about addresses might or might not be constructive, let
>>> me just point out that there is no outwardly visible distinction
>>> between "address" or "identity".
>>
>> Er, yes there is. Address refers to a position in space. Identity
>> refers to the state or quality of being identical (i.e. the same). My
>> identity remains the same as I travel from one address to another.
>
> That sounds metaphysical.
>
> What I'm looking for is snippets of Python code that illustrate the
> difference.
>
> That's how you can illustrate the difference between the "==" and "is"
> operators:
>
>     >>> ["a"] is ["a"]
>     False
>     >>> ["a"] == ["a"]
>     True

When you have an address, you can use that to locate the thing. In C,
that's pointer dereferencing. If I give you the id of a Python object,
can you locate that object and find out something about it? If you
can't, it's not an address.

And you have to make this work in Python (the language), not just
CPython (the interpreter). I'll consider an answer satisfactory if it
runs on the Big Four - CPython, Jython, IronPython, and PyPy - and
mainly, you need to show that it works in Jython, because that's the
most different.

>> Which part is unclear? The fact that f(a) returns a, or the fact that
>> `a is a` is true?
>
> In fact,
>
>     a is a
>
> would be a *great* start for a formal definition/requirement of the "is"
> operator, although you'd have to generalize it to
>
>     b is b
>     c is c
>
> etc as well. Unfortunately, when I try it, I get:
>
>     >>> a is a
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     NameError: name 'a' is not defined

And oh how terrible, Python doesn't define any other operators on
unassigned names either. The 'is' operator is looking at OBJECT
identity, so you need to have an OBJECT. If you don't understand that
part of Python's object model, I recommend learning from Ned
Batchelder:

https://nedbatchelder.com/text/names1.html

>> First part is implied by Python's execution model,
>
> [Citation needed]

https://docs.python.org/3/reference/executionmodel.html

>> and the second by the definition of the `is` operator.
>
> [Citation needed]

https://docs.python.org/3/reference/expressions.html#is-not

> There are many ways to define identity:
>
>  1. Map Python's data model to that of another programming language, for
>     example C. This technique is very common and useful. No wonder the
>     word "address" keeps popping up.

Very common, yes, but not so useful. Python's data model is not the
same as C's, and it's easier to explain without diving into the lower
level details.

Objects exist. You can ask a child about object identity and you'll
get sane responses.

* "If I put the ball behind my back, is it still a ball?"
* "If I put the ball inside this box, is it the same ball?"
* "I dip this ball in water; it is now wet. Is it still the same ball?"

No mention of pointers. No mention of C.

>  2. List a number of formal requirements: any implementation that
>     complies with the requirements is a valid implementation of the
>     language.

You want to go down that path? Okay. Start reading
https://docs.python.org/3/reference/ and let me know when you're done.
You may notice that it was easily able to supply the dolphins you
needed above.

Just don't try explaining to a novice programmer that way. It's a
waste of everyone's time.

ChrisA