443,965 Members | 1,631 Online Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

# dot operations

 P: n/a Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially when I combine couple of such operations into one line. Is it possible to overload operators, so that, I can use .+ for element wise addition, as, c = a .+ b which is much more readable. Similarly, I want to use .- , .*, ./ . Is it possible to do? thanks. - Suresh Jan 11 '07 #1
6 Replies

 P: n/a jm*******@no.spam.gmail.com wrote: Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially when I combine couple of such operations into one line. Is it possible to overload operators, so that, I can use .+ for element wise addition, as, c = a .+ b which is much more readable. Similarly, I want to use .- , .*, ./ . Is it possible to do? import numpy You'll not even need dots Jan 11 '07 #2

 P: n/a jm*******@no.spam.gmail.com wrote: Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially when I combine couple of such operations into one line. Is it possible to overload operators, so that, I can use .+ for element wise addition, as, c = a .+ b which is much more readable. Similarly, I want to use .- , .*, ./ . Is it possible to do? thanks. - Suresh List comprehensions? >>a = (1, 2, 3, 4)b = (4, 3, 2, 1)import operatorc = map(operator.add, a, b)c [5, 5, 5, 5] >>c1 = [a1+b1 for a1,b1 in zip(a,b)]c1 [5, 5, 5, 5] >>> - Paddy. Jan 11 '07 #3

 P: n/a Paddy wrote: jm*******@no.spam.gmail.com wrote: Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially when I combine couple of such operations into one line. Is it possible to overload operators, so that, I can use .+ for element wise addition, as, c = a .+ b which is much more readable. Similarly, I want to use .- , .*, ./ . Is it possible to do? thanks. - Suresh List comprehensions? >a = (1, 2, 3, 4)b = (4, 3, 2, 1)import operatorc = map(operator.add, a, b)c [5, 5, 5, 5] >c1 = [a1+b1 for a1,b1 in zip(a,b)]c1 [5, 5, 5, 5] >> I just found this from : http://aspn.activestate.com/ASPN/Coo.../Recipe/384122 class Infix(object): def __init__(self, function): self.function = function def __ror__(self, other): return Infix(lambda x, self=self, other=other: self.function(other, x)) def __or__(self, other): return self.function(other) def __rlshift__(self, other): return Infix(lambda x, self=self, other=other: self.function(other, x)) def __rshift__(self, other): return self.function(other) def __call__(self, value1, value2): return self.function(value1, value2) import operator dotplus = Infix(lambda x,y: map(operator.add, x, y)) a = range(4) b = range(4) c = a |dotplus| b > - Paddy. Jan 11 '07 #4

 P: n/a On Thursday, Jan 11th 2007 at 11:41 +0100, quoth robert: =>jm*******@no.spam.gmail.com wrote: =>Hi, => Frequently I get to do like this: =>a = (1, 2, 3, 4) # some dummy values =>b = (4, 3, 2, 1) =>import operator =>c = map(operator.add, a, b) =>> =>I am finding the last line not very readable especially when I combine =>couple of such operations into one line. Is it possible to overload =>operators, so that, I can use .+ for element wise addition, as, =>c = a .+ b =>which is much more readable. =>> =>Similarly, I want to use .- , .*, ./ . Is it possible to do? => =>import numpy => =>You'll not even need dots I'm very new so my plea to be gentle is still on. I just looked at the numpy package. Seems very cool, but for the life of me I didn't understand the method by which python allows for creation of infix operators. Can someone please explain or point me to a reference? TIA -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net Jan 11 '07 #5 