Verifiably better, validated Enum for Python

Chris Angelico rosuav at gmail.com
Wed May 24 21:26:28 EDT 2017


On Thu, May 25, 2017 at 9:34 AM, bartc <bc at freeuk.com> wrote:
> That was quite likely with older Fortrans, where subroutines only used
> pass-by-reference, but which didn't stop you passing references to constants
> that the subroutine could then modify.
>
> I doubt that's still the case. With C, however, I tried this today:
>
>   "abcdef"[3] = 'z';
>   puts("abcdef");
>
> Some versions crashed; some printed 'abczef', and some 'abcdef'. (The
> language says this is undefined, but it doesn't try too hard to stop you
> doing it.)

And why should they try to stop you? The whole point of undefined
behaviour is that you shouldn't be doing this, so if you do, the
interpreter's allowed to do anything.

# Lifted from reddit: change the value of 29 to be 100
import ctypes
def deref(addr, typ):
    return ctypes.cast(addr, ctypes.POINTER(typ))
deref(id(29), ctypes.c_int)[6] = 100

print(29)
x = 29
y = x + 1
print(y - 1)

What's going to happen? Will it print 100 twice? What is y? The
behaviour of CPython after you mess around with ctypes like this is as
undefined as any of C's weirdnesses.

And yes, Steve, this is a challenge to you: if you think C's undefined
behaviour is an abomination that should not be allowed to exist,
define what Python should do here.

ChrisA



More information about the Python-list mailing list