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
>>> 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?)
>>> 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