473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overloading assignment operator

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
12 8028
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
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
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

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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
by: jim.brown | last post by:
The attached code implements a test class to show an error in overloading operator=. This code works on Windows with Visual Studio and simpler cases work with gcc 3.3.2 on Solaris 9. On Windows,...
3
by: Abubakar | last post by:
Hi, lets say I have a class called "hashstring". I want to be able to write the following code: hashstring hs ( "hello world" ); char * somecharptr; somecharptr = hs; // here the somecharptr...
1
by: prabhatnitk | last post by:
Why can't we overload assignment operator through a friend function.
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
4
by: anaghawalvekar | last post by:
is it true that you cannot overload the assignment operator through a friend function? please give a reason for your answer.
12
by: subramanian100in | last post by:
For a class Test, we write the assignment operator as Test & Test::operator=(const Test & rhs); instead of const Test & Test::operator=(const Test & rhs); that is, the return type of...
9
osfreak
by: osfreak | last post by:
class base { public: base(int data = 0):a(data) {} base(const base& rhs) { a = rhs.a;
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.