472,378 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Re: Overloading operators

On Wed, 15 Oct 2008 14:34:14 +0200, Mr.SpOOn wrote:
in a project I'm overloading a lot of comparison and arithmetic
operators to make them working with more complex classes that I defined.
What is the best way to do this? Shall I use a lot of "if...elif"
statements inside the overloaded operator? Or is there a more pythonic
and dynamic way?
Something that is more pythonic is something that doesn't use
multimethods. It's just an elaborated way to do type checking. In python,
you usually avoid type checking and if-elif-block-with-isinstance in
favor of Duck Typing and EAFP (Easier to Ask Forgiveness than Permission,
i.e. try-block).
Sometimes I need a different behavior of the operator depending on the
argument. For example, if I compare a object with an int, I get a
result, but if I compare the same object with a string, or another
object, I get another result.
*smells a bad class design* If that is the case, I'd recommend on
splitting that behavior into two or more functions/operators (or possibly
splitting the class). It's hard to reason the behavior of a class if the
class is that complex (Simple is better than complex; Complex is better
than complicated. The Zen of Python this:2-3).

Oct 16 '08 #1
0 633

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

Similar topics

by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the...
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
by: uday | last post by:
Does C supports overloading. I am thinking no. But some people are saying yes. If yes how. Please answer with examples. Also in c++ size of empty class is one. why. and what are the default...
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I...
by: PengYu.UT | last post by:
Hi, It seems that C++ does not allow overloading operators for primative types, e.g. int, double. I'm wondering whether it is ture or there is some walk-around? Thanks, Peng #include...
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
by: Rahul | last post by:
Hi Everyone, I was overloading the operator= function as a class member function, #include <iostream.h> class A { int value; public : A& operator = (const A& ref)
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.