By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

Overloading assignment operator

P: n/a
Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.

regards,
Achim
Jan 23 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Achim Domma wrote:
I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
>>class D(dict):
.... def __setitem__(self, key, value):
.... print key, "<--", value
.... dict.__setitem__(self, key, value)
....
>>namespace = D(B=42, C=24)
exec "A = B * C" in namespace
A <-- 1008

Peter
Jan 23 '07 #2

P: n/a
On Jan 23, 12:24 pm, Achim Domma <d...@procoders.netwrote:
Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.

regards,
Achim
Simple option: how do you feel about using '<<=' instead of '=' (by
defining __ilshift__)? This gives you:

A <<= B * C

(looks sort of like "injecting" the result of B times C into A)

More complicated option: embed an expression/assignment parser into
your app. You can get a jump on this using some of the examples that
come with pyparsing (can check these out online at
http://pyparsing.wikispaces.com/Examples - look at fourFn.py and
simpleArith.py).

-- Paul

Jan 23 '07 #3

P: n/a
Achim Domma wrote:
I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.
Are you sure you even need to do that?
>>class C:
.... def __init__(self, x):
.... self.x = x
.... def __mul__(self, other):
.... return C(self.x*other.x)
....
>>result = C(2)*C(3)
print result
<__main__.C instance at 0x402e13ec>
>>result.x
6

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Life is something to do when you can't get to sleep.
-- Fran Lebowitz
Jan 23 '07 #4

P: n/a

Achim Domma schrieb:
Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
Not that I know about it but what shall be the behaviour of assignment
when being overloaded?

Kay

Jan 23 '07 #5

P: n/a
On Tue, 23 Jan 2007 19:42:01 +0100, Peter Otten wrote:
Achim Domma wrote:
>I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
>>>class D(dict):
... def __setitem__(self, key, value):
... print key, "<--", value
... dict.__setitem__(self, key, value)
...
>>>namespace = D(B=42, C=24)
exec "A = B * C" in namespace
A <-- 1008
Very clever, except:

(1) The Original Poster's requirement was for a "clean syntax" and
'exec "A = B * C" in namespace' is anything but a clean syntax.

(2) The O.P. specifies that the syntax is for use by his users. We don't
know who these users are, but can you see users getting this right and not
ignoring the namespace argument?

(3) Even if they do use the namespace argument, how hard is it for the
users to break the security of your exec?
>>exec "A = B * C;import os;os.system('ls -l break-something')" in namespace
A <-- 1008
os <-- <module 'os' from '/usr/lib/python2.4/os.pyc'>
-rw-rw-r-- 1 steve steve 0 Jan 24 08:27 break-something

Using exec on user-supplied data is just begging to be p0wned.
--
Steven.

Jan 23 '07 #6

P: n/a
Paul McGuire wrote:
Simple option: how do you feel about using '<<=' instead of '=' (by
defining __ilshift__)? This gives you:

A <<= B * C

(looks sort of like "injecting" the result of B times C into A)
Thanks! That is exactly the kind of solution I was looking for! :-)

Achim
Jan 23 '07 #7

P: n/a
Steven D'Aprano wrote:
On Tue, 23 Jan 2007 19:42:01 +0100, Peter Otten wrote:
>Achim Domma wrote:
>>I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
>>>>class D(dict):
... def __setitem__(self, key, value):
... print key, "<--", value
... dict.__setitem__(self, key, value)
...
>>>>namespace = D(B=42, C=24)
exec "A = B * C" in namespace
A <-- 1008

Very clever, except:

(1) The Original Poster's requirement was for a "clean syntax" and
'exec "A = B * C" in namespace' is anything but a clean syntax.

(2) The O.P. specifies that the syntax is for use by his users. We don't
know who these users are, but can you see users getting this right and not
ignoring the namespace argument?
I thought he might hide everything but the expression

A = B * C

from the user.
(3) Even if they do use the namespace argument, how hard is it for the
users to break the security of your exec?
>>>exec "A = B * C;import os;os.system('ls -l break-something')" in
namespace
A <-- 1008
os <-- <module 'os' from '/usr/lib/python2.4/os.pyc'>
-rw-rw-r-- 1 steve steve 0 Jan 24 08:27 break-something

Using exec on user-supplied data is just begging to be p0wned.
Yes. Unless the application is deployed to the user's machine, in which case
he has more straightforward methods to destroy his own data.

Peter

Jan 23 '07 #8

P: n/a
Achim Domma wrote:
Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.

regards,
Achim
Why do you need to overload assignment anyway? If you overloaded "*"
properly, it should return
the result you want, which you then "assign" to A as usual. Maybe I'm
missing something.

Jan 24 '07 #9

P: n/a
On Tue, 23 Jan 2007 18:07:55 -0800, Russ wrote:
Achim Domma wrote:
>Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.

regards,
Achim

Why do you need to overload assignment anyway? If you overloaded "*"
properly, it should return
the result you want, which you then "assign" to A as usual. Maybe I'm
missing something.
One common reason for overriding assignment is so the left-hand-side of
the assignment can choose the result type. E.g. if Cheddar, Swiss and
Wensleydale are three custom classes, mutually compatible for
multiplication:

B = Cheddar() # B is type Cheddar
C = Swiss() # C is type Swiss
# without overloading assignment
A = B * C # A is (possibly) Cheddar since B.__mul__ is called first
A = C * B # A is (possibly) Swiss since C.__mul__ is called first
# with (hypothetical) assignment overloading
A = B * C # A is type Wensleydale since A.__assign__ is called

Except, of course, there is no assignment overloading in Python. There
can't be, because A may not exist when the assignment is performed, and
if it does exist it might be a complete different type.

Instead, you can do something like this:

A = Wensleydale(B) * Wensleydale(C)

or

A = Wensleydale(B * C)


--
Steven D'Aprano

Jan 24 '07 #10

P: n/a
Hi thre,

On Jan 24, 5:24 am, Achim Domma <d...@procoders.netwrote:
I want to use Python to script some formulas in my application.
Depending on what you're trying to do, you might possibly find it
useful to lake a look at the approach used by PyGINAC, which is a
symbolic algebra system (in C++) wrapped with some clever python
bindings:

http://pyginac.sourceforge.net/

Hope that helps,
JP

Jan 24 '07 #11

P: n/a
Steven D'Aprano wrote:
On Tue, 23 Jan 2007 18:07:55 -0800, Russ wrote:
>Achim Domma wrote:
>>Hi,

I want to use Python to script some formulas in my application. The user
should be able to write something like

A = B * C

where A,B,C are instances of some wrapper classes. Overloading * is no
problem but I cannot overload the assignment of A. I understand that
this is due to the nature of Python, but is there a trick to work around
this?
All I'm interested in is a clean syntax to script my app. Any ideas are
very welcome.

regards,
Achim
Why do you need to overload assignment anyway? If you overloaded "*"
properly, it should return
the result you want, which you then "assign" to A as usual. Maybe I'm
missing something.

One common reason for overriding assignment is so the left-hand-side of
the assignment can choose the result type. E.g. if Cheddar, Swiss and
Wensleydale are three custom classes, mutually compatible for
multiplication:

B = Cheddar() # B is type Cheddar
C = Swiss() # C is type Swiss
# without overloading assignment
A = B * C # A is (possibly) Cheddar since B.__mul__ is called first
A = C * B # A is (possibly) Swiss since C.__mul__ is called first
# with (hypothetical) assignment overloading
A = B * C # A is type Wensleydale since A.__assign__ is called

Except, of course, there is no assignment overloading in Python. There
can't be, because A may not exist when the assignment is performed, and
if it does exist it might be a complete different type.

Instead, you can do something like this:

A = Wensleydale(B) * Wensleydale(C)

or

A = Wensleydale(B * C)

I think that's the first time I've actually seen someone use a Monty
Python theme for a python example, and I must say, I like it. However,
"We are all out of Wensleydale."

Cheers,
Cliff
Jan 29 '07 #12

P: n/a
J. Clifford Dyer wrote:
I think that's the first time I've actually seen someone use a Monty
Python theme for a python example, and I must say, I like it. However,
"We are all out of Wensleydale."

Cheers,
Cliff
Oh, then you clearly don't waste nearly enough time on this newsgroup ;-)

http://groups.google.com/group/comp....on&q=spam+eggs

http://groups.google.com/group/comp....ch?q=shrubbery

http://groups.google.com/group/comp....n&q=knights+ni

http://groups.google.com/group/comp....python&q=larch

Idly yours,

Michael

Jan 29 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.