473,395 Members | 1,987 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,395 software developers and data experts.

May i customize basic operator (such as 1==3)?

Hi,all
as we know, we can override the operator of one object(for example __eq__).
my question is, how to override the basic operator?
for example,

for any object comparison operator(including litterals),
for example,
a = "123"
b = "321"

the boolean equation a == b,
i need override "==" operator like this:
first display a and b

then return real boolean result (a == b).

thanks!
-----------------------------------------------
Best regards,
kangzz

mailto:zz******@hotmail.com
Tel : 021-65407754
MP: 13916928084

__________________________________________________ _______________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/g...ave/direct/01/

Feb 22 '06 #1
5 1256
I believe you are asking for a side effect from the "==" operator.

Add print statements to the __eq__ method.

"kanchy kang" <zz******@hotmail.com> wrote:
Hi,all
as we know, we can override the operator of one object(for example __eq__).
my question is, how to override the basic operator?
for example,

for any object comparison operator(including litterals),
for example,
a = "123"
b = "321"

the boolean equation a == b,
i need override "==" operator like this:
first display a and b

then return real boolean result (a == b).

thanks!
-----------------------------------------------
Best regards,
kangzz

mailto:zz******@hotmail.com
Tel : 021-65407754
MP: 13916928084

_________________________________________________ ________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/g...ave/direct/01/

--
Regards,
Casey
Feb 22 '06 #2
Casey Hawthorne wrote:
I believe you are asking for a side effect from the "==" operator.

Add print statements to the __eq__ method.


The things is, he wants to make those modifications to builtin types, which he
can't do.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Feb 22 '06 #3
Cannot one subclass the builtin types?

I have heard, that one should always use objects when programming and
avoid the builtin types!

Then one is prepared to change objects at will and not rely on any
special properties of the builtin types!
Robert Kern <ro*********@gmail.com> wrote:
Casey Hawthorne wrote:
I believe you are asking for a side effect from the "==" operator.

Add print statements to the __eq__ method.


The things is, he wants to make those modifications to builtin types, which he
can't do.

--
Regards,
Casey
Feb 22 '06 #4
Casey Hawthorne wrote:
Cannot one subclass the builtin types?
Of course! But that won't change the method on instances of the original builtin
type.
I have heard, that one should always use objects when programming and
avoid the builtin types!
That's not particularly good advice for Python.
Then one is prepared to change objects at will and not rely on any
special properties of the builtin types!


If you really want to do that, go ahead. I won't stop you. I won't use your
code, either.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Feb 22 '06 #5
Casey Hawthorne wrote:
I have heard, that one should always use objects when programming and
avoid the builtin types!


Who told you that? Some university teacher with no industrial
experience? ;^)

It's not good advice in Python programming, for two reasons.
- It can influence performance significantly, since Python
is so dynamic. You could change the behaviour of a class,
or change the class of an instance in runtime. This means
that the Python runtime system needs to look up a lot of
things repeatedly for class instances. For ints and floats
etc, it doesn't need to do that.
- In general, Python doesn't rely on types so much for enforcing
things. The good way to ensure correct Python code is with
automated tests, and it's considered good practice to design
your APIs so that they require as little as possible from
the parameters passed in. Stricter typing means tighter
coulpling, and thus a bigger maintenance burden.

For e.g. C++, I think it could often be a good idea to use more
specialzed classes, particularly if you want the compiler to help
you with type checking. I've rarely seen it used in practice
though. The problem with using the compiler to verify that
the code is correct is that while it can check types (and do
it better if they are more specific) it still can't verify
that the code actually does what you want it to do, just that
it does *something* which is legal C++. So, in the end you
still need automated tests to verify the program in a systematic
way, and these tests will find those type errors even if you
don't write a lot of specialized classes. I mean, if you have
a full set of tests, and all tests run OK, your program is
correct, whatever types you used in various places. It might
not be ideal, but it's correct. Your classes will mainly be a
rigid way of documenting your API.
Feb 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Kilmer | last post by:
I would like to be able to customize the path structure that VS.NET 2003 creates for projects. 1. I'd like to be able to create a project without VS.NET creating a folder for that project. ...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
3
by: sachin | last post by:
How to customize PrintPreviewDialog, such that, addition of a new button, change the functionality of existing button should be possible programatically. Sample code will help a lot. Moreover, what...
1
by: Andrew J. Marshall | last post by:
I like the automatic reformatting of code (except for the bugs), but I was wondering if anyone knew of a way to customize the effects? Andrew J. Marshall MCP (Visual Basic) Fairfax, VA
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
5
by: thinktwice | last post by:
key_compare can only return true, false , but how about equal happens?
8
by: pauldepstein | last post by:
The following code was written by a colleague -- to preserve confidentiality, the name of the class is changed: HisClass operator+ (const HisClass & h1, const HisClass & h2) { // some code here}...
13
by: Prisoner at War | last post by:
I know about the * * style="cursor: pointer; cursor: hand;" * * attribute of the <imgtag, but is there a way JavaScript can "load in" one's own graphic for such events?? How? TIA!
7
by: Gregor Kofler | last post by:
Prisoner at War meinte: (a) This has nothing to do with JS. (b) This is not a fallback. You just overwrite your custom-pointer property with the standard one. In your case you will *always*...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
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,...

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.