Overloading assignment operator

A

Achim Domma

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
 
P

Peter Otten

Achim said:
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?
.... def __setitem__(self, key, value):
.... print key, "<--", value
.... dict.__setitem__(self, key, value)
....A <-- 1008

Peter
 
P

Paul McGuire

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
 
E

Erik Max Francis

Achim said:
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?
.... def __init__(self, x):
.... self.x = x
.... def __mul__(self, other):
.... return C(self.x*other.x)
....6
 
K

Kay Schluehr

Achim said:
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
 
S

Steven D'Aprano

... def __setitem__(self, key, value):
... print key, "<--", value
... dict.__setitem__(self, key, value)
...
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?
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.
 
A

Achim Domma

Paul said:
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
 
P

Peter Otten

Steven said:
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?

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
 
R

Russ

Achim said:
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.
 
S

Steven D'Aprano

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)
 
J

John Pye

Hi thre,

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
 
J

J. Clifford Dyer

Steven said:
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
 
M

Michael Spencer

J. Clifford Dyer said:
> 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.lang.python/search?group=comp.lang.python&q=spam+eggs

http://groups.google.com/group/comp.lang.python/search?q=shrubbery

http://groups.google.com/group/comp.lang.python/search?group=comp.lang.python&q=knights+ni

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

Idly yours,

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,180
Latest member
CryptoTax Software
Top