I've got the action '+' stored as the variable 'plus',
that is plus='+'
Now I want to use the variable plus in an equation:
5 plus 7, just line 5 + 7
How do I do that?
Dan 4 1783
Foolish me; I examined the __builtin__ and found __getattribute_ _,
along with '__mul__' '__add__', '__sub__' and '__div__'.
So using a builtin object like 'x' ...
x = 5
attr = '__mul__'
print x.__getattribut e__( attr )( 5 )
25
My program will store the operand as '__mul__' instead of '*', along with
a health bit of comment.
Dan. Closed.
On Fri, 05 Nov 2004 08:01:59 +0000, John Doe wrote: I've got the action '+' stored as the variable 'plus', that is plus='+'
Now I want to use the variable plus in an equation: 5 plus 7, just line 5 + 7
How do I do that?
Dan
John Doe wrote: Foolish me; I examined the __builtin__ and found __getattribute_ _, along with '__mul__' '__add__', '__sub__' and '__div__'.
So using a builtin object like 'x' ...
x = 5 attr = '__mul__'
print x.__getattribut e__( attr )( 5 ) 25
My program will store the operand as '__mul__' instead of '*', along with a health bit of comment.
However, this may sometimes fail: 1 + 1.0
2.0 1 .__add__(1.0)
NotImplemented
Consider using the operator module instead:
import operator operator.add(1, 1.0)
2.0 getattr(operato r, "add")(1, 1.0)
2.0
Peter
John Doe <atterdan <at> yahoo.com> writes: So using a builtin object like 'x' ...
x = 5 attr = '__mul__'
print x.__getattribut e__( attr )( 5 ) 25
My program will store the operand as '__mul__' instead of '*', along with a health bit of comment.
You also might consider just storing the operator.<op> methods in an appropriate
dictionary: import operator operator_map = {'+':operator.a dd, '-':operator.sub} operator_map['+'](1, 2.0)
3.0
This allows you to call the add function whatever you want.
Steve
John Doe <at******@yahoo .com> wrote in message news:<pa******* *************** ******@yahoo.co m>... I've got the action '+' stored as the variable 'plus', that is plus='+'
Now I want to use the variable plus in an equation: 5 plus 7, just line 5 + 7
How do I do that?
Dan
If you want a string:
equation = '5 '+plus+' 7'
or
equation = '5 %s 7' % plus
Regards
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: stanlo |
last post by:
hi to everyone, this is still a follow up of my project ,mathematical
expression.this project is meant to evaluate mathemtical expressions
with oparators,+,-,*,/.more than two operands can be done, eg it should
be able to do,1+9-45*7/12,or 4* 5+6*21.from reading and help from you i
have been able to write the program.but my problem is to...
|
by: Neil Schemenauer |
last post by:
The title is perhaps a little too grandiose but it's the best I
could think of. The change is really not large. Personally, I
would be happy enough if only %s was changed and the built-in was
not added. Please comment.
Neil
PEP: 349
Title: Generalised String Coercion
|
by: Don Starr |
last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?
For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)
char x = "abcd";
printf(...
|
by: Timo |
last post by:
I am trying to get address of myStruct to a string array texts.
I am using M$ Visual C++ 6.0 (this is not OS specific question, though,
this code should also work on 16 bit embedded compiler ;)).
This is my code:
typedef struct
{
const unsigned short a;
const unsigned long b;
|
by: junky_fellow |
last post by:
char *str1 = "Hello";
char arr1 = { "Hello" };
char arr2 = { 'H', 'e', 'l', 'l', 'o' };
Is it legal to modify str1, arr1 and arr2 ?
| |
by: Catalin Porancea |
last post by:
Whenever I try this:
____________________________________________________________________
Dim cmd_sp_payments As SqlCommand = New SqlCommand("sp_payments", con)
cmd_sp_payments.CommandType = CommandType.StoredProcedure
Dim CustID As SqlParameter = cmd_sp_payments.Parameters.Add("@cust_id",
SqlDbType.Text)
CustID.Value = strCustID
Dim...
|
by: hn.ft.pris |
last post by:
Hi:
I'm a beginer of STL, and I'm wondering why none of below works:
########################################################################
..........
string str("string");
if ( str == "s" ) cout << "First character is s" << endl;
OR:
string str("string");
string::iterator it = str.begin();
|
by: somenath |
last post by:
Hi All,
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various...
|
by: WP |
last post by:
Hello! I need some help with my program...it's supposed to read infix
expressions line by line from stdin and each expression should be
divided into operands and operators and added to a vector of strings. So
if we read one line that holds "1+2" the vector should afterwards hold
the strings "1", "+" and "2".
Valid operators are +, -, * and /...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |