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

K

kanchy kang

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:[email protected]
Tel : 021-65407754
MP: 13916928084

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
 
C

Casey Hawthorne

I believe you are asking for a side effect from the "==" operator.

Add print statements to the __eq__ method.
 
R

Robert Kern

Casey said:
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
(e-mail address removed)

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

Casey Hawthorne

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!
 
R

Robert Kern

Casey said:
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
(e-mail address removed)

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

Magnus Lycka

Casey said:
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.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top