Skip to main content

Undefined Python

Will McGugan has a post about the smell of languages, using an example of code from C that is actually completely undefined: no one knows what it should do. The code in question is this:

int main() { int i = 5; i = ++i + ++i; printf ("%d", i); }
One of the commenters talks about how some constructs in Python can be undefined, but I disagree. My response is below.

There can certainly be some cases where you aren’t sure about things involved in Python, but nothing undefined in the sense that C can be. I can counter the examples given.

“While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined.”

- While the list is being sorted, any inspection or mutation could only be occuring in some other thread. Multiple threads are, by desogn, indeterminable.

“Formfeed characters occurring elsewhere in the leading whitespace have an undefined effect (for instance, they may reset the space count to zero).”

- This is caused by improper formatting of a text file. If the file is not formatted properly, you can’t expect magic its-OK-ness.

“super is undefined for implicit lookups using statements or operators such as “super(C, self)[name]””

- Undefined? I actually don’t agree. At least, not by the term “undefined” as used in this post. implicit lookups like this are looked up on the type of the object in question, which is the super builtin type, in this case. The methods are undefined in the sense that the type does not define them, so they don’t exist. You can’t look them up. This is not “undefined” as in not knowing the behavior.

“If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen.”

- This is about private name mangling. The mangled names should be considered an implementation detail, you should never use or try to create the names manually, so any implementation specific differences are completely irrelevant.





Comments

Brian said…
You don't have to be in another thread to mutate a list when sorting. Consider things like the key or cmp arguments to list.sort. These can execute arbitrary code during the sort, so it would be perfectly possible (though rather silly) to give a callback that mutates the list.
Stan Seibert said…
Suppose you have a special cmp_using_list function which takes a list to define an ordering of arbitrary objects:

>>> cmp_using_list = lambda l,x,y: cmp(l.index(x), l.index(y))
>>> foo, bar, baz = list(), '', dict() # arbitrary objects
>>> ordering = [foo, bar, baz]
>>> mycmp = lambda x,y: cmp_using_list(ordering, x, y)
>>> mycmp(foo,foo)
0
>>> mycmp(foo,bar)
-1
>>> mycmp(baz,foo)
1
>>> sorted([foo, foo, baz, bar, baz, foo], cmp=mycmp)
[[], [], [], '', {}, {}]

So far so good, but now this statement is undefined:

>>> sorted(ordering, cmp=mycmp)

mycmp accesses ordering while ordering is being sorted. Of course, by construction, ordering is already sorted, so this happens to work anyway. (And, from a practical perspective, why would you ever need to do this?)
Stan Seibert said…
Hah, figures I would screw that example up. Of course, I should be using sort() instead of sorted() as sorted constructs a new list, thereby eliminating the self reference. If you try it, things do blow up:
>>> ordering.sort(cmp=mycmp)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
File "<stdin>", line 1, in <lambda>
ValueError: list.index(x): x not in list
Calvin Spealman said…
I found out why Stan's example blows up when I was toying with this myself. It looks like during sorting, the list is empty (at least in 2.5.1 CPython, so don't depend on it). Its probably an implementation that empties the list, holds an array of object pointers, sorts them, and refills the array. I suppose its to avoid the heavy GIL locking that would be needed to continually shift things about inside the list.
Marius said…
How about a simple example: the order of keys returned by a_dict.keys() is not defined in Python.

Popular posts from this blog

Respect and Code Reviews

Code Reviews in a development team only function best, or possible at all, when everyone approaches them with respect. That’s something I’ve usually taken for granted because I’ve had the opportunity to work with amazing developers who shine not just in their technical skills but in their interpersonal skills on a team. That isn’t always the case, so I’m going to put into words something that often exists just in assumptions.
You have to respect your code. This is first only because the nature and intent of code reviews are to safeguard the quality of your code, so even having code reviews demonstrates a baseline of respect for that code. But, maybe not everyone on the team has the same level of respect or entered a team with existing review traditions that they aren’t acquainted with.
There can be culture shock when you enter a team that’s really heavy on code reviews, but also if you enter a team or interact with a colleague who doesn’t share that level of respect for the process or…

CARDIAC: The Cardboard Computer

I am just so excited about this.


CARDIAC. The Cardboard Computer. How cool is that? This piece of history is amazing and better than that: it is extremely accessible. This fantastic design was built in 1969 by David Hagelbarger at Bell Labs to explain what computers were to those who would otherwise have no exposure to them. Miraculously, the CARDIAC (CARDboard Interactive Aid to Computation) was able to actually function as a slow and rudimentary computer. 
One of the most fascinating aspects of this gem is that at the time of its publication the scope it was able to demonstrate was actually useful in explaining what a computer was. Could you imagine trying to explain computers today with anything close to the CARDIAC?

It had 100 memory locations and only ten instructions. The memory held signed 3-digit numbers (-999 through 999) and instructions could be encoded such that the first digit was the instruction and the second two digits were the address of memory to operate on. The only re…

How To Care If BSD, MIT, or GPL Licenses Are Used

The two recent posts about some individuals' choice of GPL versus others' preference for BSD and MIT style licensing has caused a lot of debate and response. I've seen everything as an interesting combination of very important topics being taken far too seriously and far too personally. All involved need to take a few steps back.

For the uninitiated and as a clarifier for the initiated, we're dealing with (basically) three categories of licensing when someone releases software (and/or its code):
Closed Source. Easiest to explain, because you just get nothing.GPL. If you get the software, you get the source code, you get to change it, and anything you combine it with must be under the same terms.MIT and BSD. If you get the software, you might get the source code, you get to change it, and you have no obligations about anything else you combine it with.The situation gets stickier when we look at those combinations and the transitions between them.

Use GPL code with Closed S…