![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Expert Programmer
|
Quote:
GJ
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
|
#12 |
|
Newbie
|
This is a very nice article, very insightful. File > Print... (select printer) press Enter Thank you
![]()
__________________
War cannot be avoided until the physical cause for its recurrence is removed and this, in the last analysis, is the vast extent of the planet on which we live. ~ Nikola Tesla |
|
|
|
|
|
#13 | ||
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
def add(a, b):
return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a * b
ops = {"+" : add, "-" : sub, "*" : mul, "/" : div}
def do_operation(op, a, b):
return ops[op](a, b)Quote:
|
||
|
|
|
|
|
#14 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
import operator
def do_operation(op, a, b):
return {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.div}[op](a, b)
print do_operation('+', 2, 5)--OH. |
|
|
|
|
|
|
#15 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
class Accumulator(object):
def __init__(self, start=0):
self.val = start
def __call__(self, delta=1):
self.val += delta
return self.val
def __str__(self):
return str(self.val)
x = Accumulator()
y = Accumulator(4)
print x, y
print x(2), y()
print x(1), y(-2)
print x, y
print x(), y() |
|
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
A couple more interesting tricks in Python 2.4:
Generator Comprehensions Exactly like a list comprehension, but returns a generator object instead of a list. e.g: (i for i in range(10) if (i % 2) == 0) Decorators Syntaxtic sugar for wrapping a function in another. e.g: def bar(f): ... @bar def foo(x): ... def bar(f): ... def foo(x): ... foo = bar(foo) |
|
|
|
|
|
#17 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
/me doesn't like the decorator syntax :-(
Guido's call (and the communities, there was a vote if i'm not mistaken), but I would have liked something like def static foo(x): ... |
|
|
|
|
|
#18 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
I'm with you there. The decorator syntax sucks
- and doesn't actually reduce your linecount, either. |
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Thanks Cerulean and hydroxide for the "replacement of lambda" suggestions.
Here is my code to time map() versus list comprehension: # 85% of the time map() beats list comprehension by 5 - 10% on Windows XP Python 2.4.1
import timeit
tt = timeit.Timer('map(str, [2, 3, 5, 1])')
te = (1000000 * tt.timeit(number=100000)/100000)
print "Function map(str, [2, 3, 5, 1]) uses %0.5f ms/pass" % te
tt = timeit.Timer('[str(x) for x in [2, 3, 5, 1]]')
te = (1000000 * tt.timeit(number=100000)/100000)
print "LC [str(x) for x in [2, 3, 5, 1]] uses %0.5f ms/pass" % te
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#20 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
If you're going to be calling a function in each iteration in the list then map is probably faster, yes. It's the fact that list comprehensions allow you to get away with not having to call a function that makes them faster for a bunch of situations
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|