| gmcnaughton ( @ 2008-06-11 23:00:00 |
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?
Was
Lambdas allow you to define anonymous functions with lexical closures. They're very convenient, assuming all your code fits on one line!
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:
Oh. Well, there goes that solution that works for everyone else. Thanks, 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!