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 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
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
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
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
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.
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
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
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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,...
|
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...
|
by: prabhatnitk |
last post by:
Why can't we overload assignment operator through a friend function.
|
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...
|
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.
|
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...
|
by: osfreak |
last post by:
class base
{
public:
base(int data = 0):a(data)
{}
base(const base& rhs)
{
a = rhs.a;
|
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: 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...
|
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: 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...
|
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,...
|
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...
| |