Differences between revisions 1 and 2
Revision 1 as of 2011-03-26 21:20:06
Size: 999
Editor: cdm-75-109-95-183
Comment:
Revision 2 as of 2011-03-26 23:04:43
Size: 2026
Editor: PaulBoddie
Comment: Added possible answer.
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
= Asking for Help: ... = = Asking for Help: Defining rules for adding two different data types =
Line 7: Line 7:

----

You do know that Python has a complex number type? For example:

{{{#!python numbers=disable
1 + 1j
# gives (1+1j) - a complex number
}}}

However, you could certainly define your own for educational purposes. It sounds like you support addition where a complex number instance is on the left hand side of an addition operation, but not on the right hand side. What happens when Python tries to perform an addition operation is this:

 1. Python looks for a `__add__` method on the left hand side operand. If this isn't found or if it returns `NotImplemented` (the method doesn't want to add the right hand side operand to itself), then...
 1. Python looks for a `__radd__` method on the right hand side operand. If this isn't found or doesn't want to use the left hand side operand, then you get a `TypeError`.

So perhaps your class needs to define the `__radd__` method. -- PaulBoddie <<DateTime(2011-03-27T00:04:42+0100)>>
Line 20: Line 36:
CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help: Defining rules for adding two different data types

I'm a beginner with Python and am trying to create a Complex number class. The problem I am having is as follows, if z is a complex number (or other special type) how can I find 1+z? I am able to handle z+1, but it would be much nicer to have the operation be commutative. Whenever I attempt to compute 1+z I receive a TypeError.


You do know that Python has a complex number type? For example:

1 + 1j
# gives (1+1j) - a complex number

However, you could certainly define your own for educational purposes. It sounds like you support addition where a complex number instance is on the left hand side of an addition operation, but not on the right hand side. What happens when Python tries to perform an addition operation is this:

  1. Python looks for a __add__ method on the left hand side operand. If this isn't found or if it returns NotImplemented (the method doesn't want to add the right hand side operand to itself), then...

  2. Python looks for a __radd__ method on the right hand side operand. If this isn't found or doesn't want to use the left hand side operand, then you get a TypeError.

So perhaps your class needs to define the __radd__ method. -- PaulBoddie 2011-03-26 23:04:42

When answering questions, add the CategoryAskingForHelpAnswered category when saving the page. This will move the link to this page from the questions section to the answers section on the Asking for Help page.


CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/Defining rules for adding two different data types. (last edited 2011-03-26 23:04:43 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.