osdir.com


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

About the implementation of del in Python 3


Steve D'Aprano <steve+python at pearwood.info>:

> An address is a concrete location or place, in other words a physical
> position in some space, while identity is the abstract state or
> quality of being identical (sameness), in other words a state of
> being.

Whether id() returns one such thing or not can't be discerned by a
Python program. What's more, for any compliant implementation of id(),
you can interpret the returned number as an address in some address
space (whether it's useful or not to interpret it that way).

> In full generality, I doubt that there is any definition of "identity"
> which can be fully satisfactory, but in the context of Python we can
> be more specific:
>
> Nominally two objects are identical (have the same identity) if they
> are one and the same object, and are not identical if they are
> different objects.

I believe identity can be defined much better, in numerous isomorphic
ways in fact.

For example, we could equate each object with a sequence number
(unrelated with its id()). You can define that the "None" object is in
fact the natural number 0. The "False" object is in fact the natural
number 1 etc for all the primordial objects. During the execution of the
program, new objects are created, which simply associates
characteristics to ever higher natural numbers.

That kind of mathematical treatment would be precise, but alas, hardly
very helpful for beginning programmers.

That's why I proposed the Puppy Data Model for Python months back. It
involved puppies, leashes and pegs.

>> 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.
>
> Why do you have to generalise it? The name "a" here stands in for any
> reference to any object.

"Any reference to any object" is difficult to define syntactically as
the reference itself might perform some dunder magic.

For example, you can't say that necessarily

   x.y is x.y

>> 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
>
> Now you're just trolling.

No, just trying to emphasize the difference between syntax and semantics.

   "a is a"

is a specific, legal statement in Python.

We could generalize and say (wrongly) that

   "X is X"

where X stands for any legal Python expression, is always true. However,
X might be undefined or produce side effects.

Or maybe you want to say:

   ?X ? ? ?Y ? ? ?(concat(X, "is", Y)) = 1 ? ?(X) ? ?(Y)

where ? is the set of legal Python expressions. That would reduce the
problem to defining the interpretation function ?. That's probably
necessary anyway.

>>> and the second by the definition of the `is` operator.
>> 
>> [Citation needed]
>
> Now you're taking the piss. Do I have to look everything up for you?
>
> https://docs.python.org/3/reference/expressions.html#is
>
> "x is y is true if and only if x and y are the same object."

I suppose what is meant is the above:

   ?X ? ? ?Y ? ? ?(concat(X, "is", Y)) = 1 ? ?(X) ? ?(Y)


Marko