The end to all language wars and the great unity API to come!

Corey Richardson kb1pkl at aim.com
Tue Jul 5 18:04:32 CEST 2011


Excerpts from rantingrick's message of Tue Jul 05 07:42:39 -0400 2011:
>
> I was thinking more about this comment and it occurred to me that
> Python does have user controlled data structures. Just because there
> is no "top level syntax" like ruby does not mean these do not exists.
> You only have to look below the surface. Take the sort methods of
> lists for example...
> 
> >>> lst = [
>     (100, 0),
>     (25, 2),
>     (10,1),
>     ]
> >>> lst
> [(100, 0), (25, 2), (10, 1)]
> >>> lst.sort()
> >>> lst
> [(10, 1), (25, 2), (100, 0)]
> >>> lst.sort(lambda x,y: cmp(x[1], y[1]))
> >>> lst
> [(100, 0), (10, 1), (25, 2)]
> 
> ...that looks like a "user defined control" structure to me. So how
> can an anti-feature become part of the language proper? (devils
> advocate here) :)
> 

How is giving the sort method a function by which to determine the relative
value of objects a control structure? Do you know what a control structure is?
It's something that you use to modify control flow:

if foo <= bar:
	foo += 1
else:
	bar += 1

That's a control structurem the "if-else". I don't know what Ruby calls a 
control structure, but that's what it is. for and while are in there too.
When you run lst.sort(lambda x, y: cmp(x[1], y[1])), what happens?

We'll call that argument srt, here's a sample (naive) implementation:

def sort(key):
	lst = self.internal_list
	for n in range(len(self.internal_list) - 1):
		for i in range(len(self.internal_list) - 1):
			if srt(lst[i], lst[i+1]) < 0:
				lst[i], lst[i+1] = lst[i+1], lst[i]

Untested, probably doesn't work either. See what's in there? An if. Nothing
"user-defined" at all. Sure, WHAT the if does is user-controlled with the
key, but that doesn't make that particular if a new control structure, and
it certainly doesn't make the key a control structure. You can pass a key
to a sort even in C and that certainly doesn't have user defined control
structures.
-- 
Corey Richardson
  "Those who deny freedom to others, deserve it not for themselves"
     -- Abraham Lincoln
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20110705/e1326267/attachment.sig>


More information about the Python-list mailing list