gmcnaughton ([info]gmcnaughton) wrote,
@ 2008-06-11 23:00:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Adventures in Python
I've been writing in Python for the last couple weeks to make use of the Google App Engine. I have a couple of questions for you l33t Python hax0rs out there:

1. append() vs. pop()

What bright-eyes named these functions?
array = [1, 2, 3]
array.append(4)   # add to end
array.pop()       # remove from end

Was push() a reserved keyword already? Would it have been too easy to remember? Too consistent maybe? I know naming is hard, but come on.
"6. Use opposites precisely. For every Open(), there should be a Close(); for every Insert(), a Delete(); for every Start(), a Stop()."
2. Multi-line lambdas

Lambdas allow you to define anonymous functions with lexical closures. They're very convenient, assuming all your code fits on one line!
# Simple sort using lambda
array = [1, 3, 2]
array.sort(lambda a,b: cmp(a,b))

# More complex sort; must use a locally defined function 
# because lambdas can only be one line long.
def complexFn(a,b):
  if foo(a, b):
    return -1
  elif bar(a, b):
    return 1
  return 0

array.sort(complexFn);

I can't help but think that Python shot itself in the foot syntactically here. From PEP 3099: Things that will Not Change in Python 3000:
"Adding support for statements...would require allowing multi-line lambda expressions, which would mean a multi-line expression could suddenly exist. That would allow for multi-line arguments to function calls, for instance. That is just plain ugly."
Because Python relies on indentation to define lexical blocks, multiline lambdas don't look right. However, in my defense, it's not my fault Python decided to make whitespace significant. After all, I wonder how Javascript and Ruby get around this restriction... oh right, braces!
>>> from __future__ import braces
  File "", line 1
SyntaxError: not a chance

Oh. Well, there goes that solution that works for everyone else. Thanks, Python!



(7 comments) - (Post a new comment)


[info]transiit
2008-06-12 08:10 am UTC (link)
It's fun to watch the extremes.

Python: It's right because we say so. There's one way to do it.
PHP: We thought of a way, so thus it's right. If this one doesn't work, look for one of the many other ways we thought of before this.
Perl: Well, Larry says it's right. Maybe we can convince him otherwise.

(Reply to this)

append
(Anonymous)
2008-06-12 11:46 am UTC (link)
[1, 2, 3] is a mutable list, not a pile.
append and remove are list methods from the beggining, pop was added latter because many people use list as piles.

(Reply to this) (Thread)

Re: append
(Anonymous)
2008-06-12 05:38 pm UTC (link)
Understood, however, why not just make push() an alias for append() at the same time as adding pop()? The mere existence of pop made me assume there was a push, and not finding one was counter-intuitive. Where's the symmetry?

(Reply to this) (Parent)(Thread)

Re: append
[info]paddy3118
2008-06-12 05:59 pm UTC (link)
Python tends to limit aliases. Here it is enforced over a wish for symmetry.

Note though that append is to the end (largest index), whereas push is usually to the bottom of a list. Python lists have the insert method which covers the push functionality:

>>> l = range(3)
>>> l
[0, 1, 2]
>>> l.insert(0,'X')
>>> l
['X', 0, 1, 2]
>>> l.insert(len(l),'Y')
>>> l
['X', 0, 1, 2, 'Y']
>>>

- Paddy.

(Reply to this) (Parent)(Thread)

Re: append
[info]gmcnaughton
2008-06-13 04:02 am UTC (link)
Thanks for the info on insert, at least there's symmetry there!

For push, I guess I was thinking of Perl and Ruby, in which push/pop effect the end of the list, vs. shift/unshift which effect the beginning.

(Reply to this) (Parent)


[info]jephly
2008-06-13 06:27 am UTC (link)
Here's a related conundrum that I encountered recently:

"Append" is typically an operation on a list that adds an item to the *end*.
"Push" is typically an operation on a stack that adds an item to the *top*.

Now, let's pretend that some confused API developer decided to put both operations on the same object, which we'll call a "lack" (because "stist" is hard to say).
Question: Does the "top" of this collection correspond with its "beginning" or its "end"?
Answer: Silly API developer, your object is trying to be too many things at once.

Bonus question: How does the STL tackle this issue?
Hint: You add to the end of the list with the Oh-So-Concisely-Named "push_back" method.

(Reply to this) (Thread)


[info]gmcnaughton
2008-06-13 06:38 am UTC (link)
Haha, I like it! 'lack' is an excellent data structure name; think of the puns! (You want it to do what? Oh yeah, it doesn't support that - by design!)

In Perl, push effects the end of the list because lists are implemented as dynamic arrays. Under the hood, Perl leaves some space "on the left" for a reasonable number of shifts/unshifts (5?), but it's still much more efficient to expand to the right. I guess some schmancy ring buffer could avoid the problem.

(Reply to this) (Parent)


(7 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…