Ruby offers very nice language constructs for loops or for iterating over lists like the following: 5.times { print "Hello World" }. This recipe shows a hack how to implement these features in python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Times:
def __rmul__(self,n):
for i in range(n):
self.func()
def __call__(self,func):
self.func=func
return self
times=Times()
class Each:
def __rmul__(self,L):
return map(self.func,L)
def __call__(self,func):
self.func=func
return self
each=Each()
class Length:
def __rmul__(self,L):
return len(L)
length=Length()
def printf(x):
print x
5 *times(lambda: printf("Hello"))
[1,2,3,4] *each(lambda x: printf("Count:"+str(x)))
print [1,2,3,4,5] *length
['a','b','c','d','e'] *each(lambda char: char+'!')
|
This is only a fun recipe and you should not use this programming style because it can lead to a lot of confusions. Using this style you can define methods for classes that are normally capsuled like numbers and lists. I would wish python would provide a nicer way to do this.
Some improvements. Nice (although map can do nearly the same), here a small improvement:
Frankly, Python is the best for me and I can't imagine a
better way to do language wraps like in Python.
Paul