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 6 1541 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 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 operator c = 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.
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 operator c = 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.
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
On Thu, 11 Jan 2007 06:06:39 -0800, jm*******@no.spam.gmail.com 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.
[snip]
I just found this from : http://aspn.activestate.com/ASPN/Coo.../Recipe/384122
class Infix(object):
[snip code]
c = a |dotplus| b
Personally, I find that to be the least readable of all the alternatives.
It is also slow, so slow that using it is (in my opinion) a pessimation
rather than an optimization.
Let me make the usual warnings about premature optimization being
the root of all evil, blah blah blah. Readability counts, and nobody cares
if you shave one hundredth of a millisecond off some code that your
program runs once. But, having said that, map is *fast*, especially with
the functions from operator.
>>import timeit timeit.Timer("map(operator.add, a, b)",
.... "import operator; a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
4.0072550773620605
It is even faster if you get rid of the dot lookup:
>>timeit.Timer("map(add, a, b)", "from operator import add; "
.... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
3.6557090282440186
Using lambda can be much slower:
>>timeit.Timer("map(lambda x,y: x+y, a, b)",
.... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
6.1221940517425537
A list comprehension is in the middle, speed-wise:
>>timeit.Timer("[x+y for x,y in zip(a,b)]",
.... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
5.0318419933319092
But the Infix recipe is slower than a wet week:
>>timeit.Timer("a|dotplus|b",
.... "a = (1, 2, 3, 4); b = (4, 3, 2, 1); "
.... "from __main__ import dotplus").timeit()
15.195909976959229
Speaking for myself, I find list comprehensions to be the most readable of
the alternatives and the most Pythonic.
--
Steven.
On Jan 11, 10:21 am, "Steven W. Orr" <ste...@syslang.netwrote:
On Thursday, Jan 11th 2007 at 11:41 +0100, quoth robert:
=>jm.sur...@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?
Python doesn't allow the creation of new operators, but you can
overload the existing ones (except for "and" and "or"). This is done
by implementing the methods __add__, __sub__, __mul__, etc. http://docs.python.org/ref/specialnames.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Leslaw Bieniasz |
last post by:
Cracow, 20.09.2004
Hello,
I need to implement a library containing a hierarchy of classes
together with some binary operations on objects. To fix attention,
let me assume that it is a...
|
by: Scott Brady Drummonds |
last post by:
Hello, all,
My most recent assignment has me working on a medium- to large-sized
Windows-based C++ software project. My background is entirely on UNIX
systems, where it appears that most of my...
|
by: ThunderMusic |
last post by:
Hi,
I have 2 UInt64 to add and then divide the result by another value. How can
I do this? because the math operators have not been defined for UInt64.
Can somebody help please?
Thanks
...
|
by: Chad Myers |
last post by:
I've been perf testing an application of mine and I've noticed that there
are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String
instances being created.
I've done some...
|
by: Immanuel Goldstein |
last post by:
Obtained under the Freedom of Information Act by the National Security Archive
at George Washington University and posted on the Web today, the 74-page
"Information Operations Roadmap" admits that...
|
by: mrby |
last post by:
Hi,
Does anyone know of any link which describes the (relative)
performance of all kinds of C operations? e.g: how fast is "add"
comparing with "multiplication" on a typical machine.
Thanks!...
|
by: Hallvard B Furuseth |
last post by:
I'm wondering how to design this:
An API to let a request/response LDAP server be configured so a
user-defined Python module can handle and/or modify some or all
incoming operations, and later...
|
by: carsonbj |
last post by:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant...
|
by: alex |
last post by:
hi friends ...
i am facing a problem while detecting floating point operations in my
project, please help me.
i want to find out the places in my C/C++ project where i am doing
floating...
|
by: tabassumpatil |
last post by:
Please send the c code for:
1.STACK OPERATIONS :
Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2.
2.QUEUE OPERATIONS :
Write a program to implement...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |