implementation of "complex" type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ

    #1

    implementation of "complex" type

    I tried the following:
    [color=blue][color=green][color=darkred]
    >>> x = complex(4)
    >>> y = x
    >>> y *= 2
    >>> print x, y[/color][/color][/color]
    (4+0j) (8+0j)

    But when I tried the same thing with my own class in place of
    "complex" above, I found that both x and y were doubled. I'd like to
    make my class behave like the "complex" class. Can someone tell me the
    trick? Also, where can I find the code for for the "complex" class? I
    hope it's written in Python! Thanks.

  • Mikael Olofsson

    #2
    Re: implementation of "complex&q uot; type

    Russ wrote:[color=blue][color=green][color=darkred]
    >>>>x = complex(4)
    >>>>y = x
    >>>>y *= 2
    >>>>print x, y[/color][/color]
    >
    > (4+0j) (8+0j)
    >
    > But when I tried the same thing with my own class in place of
    > "complex" above, I found that both x and y were doubled. I'd like to
    > make my class behave like the "complex" class. Can someone tell me the
    > trick? Also, where can I find the code for for the "complex" class? I
    > hope it's written in Python! Thanks.[/color]

    Have your __imul__ and such return new objects, and not perform in-place
    modification if you do not want x and y to refer to the same object
    after y*=2.

    /MiO

    Comment

    • Juho Schultz

      #3
      Re: implementation of "complex&q uot; type

      Russ wrote:[color=blue]
      > I tried the following:
      >
      >[color=green][color=darkred]
      >>>>x = complex(4)
      >>>>y = x
      >>>>y *= 2
      >>>>print x, y[/color][/color]
      >
      > (4+0j) (8+0j)
      >
      > But when I tried the same thing with my own class in place of
      > "complex" above, I found that both x and y were doubled. I'd like to
      > make my class behave like the "complex" class. Can someone tell me the
      > trick? Also, where can I find the code for for the "complex" class? I
      > hope it's written in Python! Thanks.
      >[/color]

      This is like the difference of tuples and lists.

      Your own class is mutable.
      y=x # Names x and y are now bound to the same object.
      y*=2 # change the object bound to names x and y.

      Builtin complex is immutable, so you can not manipulate the contents.
      y*=2 # creates a new object (value = 2*y), binds it to name y.

      Comment

      • Steven D'Aprano

        #4
        Re: implementation of "complex&q uot; type

        On Thu, 09 Mar 2006 01:15:57 -0800, Russ wrote:
        [color=blue]
        > I tried the following:
        >[color=green][color=darkred]
        >>>> x = complex(4)
        >>>> y = x
        >>>> y *= 2
        >>>> print x, y[/color][/color]
        > (4+0j) (8+0j)
        >
        > But when I tried the same thing with my own class in place of
        > "complex" above, I found that both x and y were doubled. I'd like to
        > make my class behave like the "complex" class. Can someone tell me the
        > trick? Also, where can I find the code for for the "complex" class? I
        > hope it's written in Python! Thanks.[/color]

        Or, to put it another way:

        "Here's some code that works!

        Now, I've written some *other* code, which has a bug in it. Who'd like to
        guess what the bug is?"

        Why don't you show us your complex class?

        Also, why are you re-inventing the wheel? Is it just to learn the
        language? Or because you want to add extra functionality? Or just NIH
        Syndrome? If it is a learning exercise, they I applaud you; if it is to
        add extra functionality, there is a better way.



        --
        Steven.

        Comment

        • cfbolz@googlemail.com

          #5
          Re: implementation of "complex&q uot; type

          Hi!

          Russ wrote:[color=blue]
          > I tried the following:
          >[color=green][color=darkred]
          > >>> x = complex(4)
          > >>> y = x
          > >>> y *= 2
          > >>> print x, y[/color][/color]
          > (4+0j) (8+0j)
          >
          > But when I tried the same thing with my own class in place of
          > "complex" above, I found that both x and y were doubled. I'd like to
          > make my class behave like the "complex" class. Can someone tell me the
          > trick? Also, where can I find the code for for the "complex" class? I
          > hope it's written in Python! Thanks.[/color]

          In CPython it is actually written in C, implemented in the
          Objects/complexobject.c file. See for example:



          In PyPy it is indeed a pure Python implementation right now:



          Cheers,

          Carl Friedrich Bolz

          Comment

          • Russ

            #6
            Re: implementation of "complex&q uot; type

            "Why don't you show us your complex class?"

            Because I don't have a complex class. I merely used the complex class
            as an example to test the referencing behavior. Please read more
            carefully next time.

            Comment

            • AndrĂ©

              #7
              Re: implementation of "complex&q uot; type

              Russ wrote:[color=blue]
              > "Why don't you show us your complex class?"
              >
              > Because I don't have a complex class. I merely used the complex class
              > as an example to test the referencing behavior. Please read more
              > carefully next time.[/color]

              I'm not the one you were replying to, but I would say to you...
              "Please write more carefully next time".

              You wrote:[color=blue]
              >But when I tried the same thing with my own class in place of
              >"complex" above, I found that both x and y were doubled.[/color]

              Why don't you show us your own
              whatever_it_is_ named_except_it _is_not_complex class?

              When you are the one seeking help, you may find it helpful to be
              polite.... I have found that comp.lang.pytho n has some of the most
              friendly and helpful people around. They will undoubtably help you (as
              they helped me many times), if you provide answers to the questions
              they ask in trying to help you...

              André

              Comment

              • Russ

                #8
                Re: implementation of "complex&q uot; type

                "When you are the one seeking help, you may find it helpful to be
                polite.... I have found that comp.lang.pytho n has some of the most
                friendly and helpful people around. They will undoubtably help you (as
                they helped me many times), if you provide answers to the questions
                they ask in trying to help you... "

                Asking sarcastically why I am "reinventin g the wheel" is a funny way of
                being friendly. I said absolutely nothing to indicate that I had
                re-implemented my own version of complex. And, by the way, even if I
                had, so what? Why is it even worth commenting on?

                Comment

                • Diez B. Roggisch

                  #9
                  Re: implementation of "complex&q uot; type

                  > Asking sarcastically why I am "reinventin g the wheel" is a funny way of[color=blue]
                  > being friendly. I said absolutely nothing to indicate that I had
                  > re-implemented my own version of complex. And, by the way, even if I
                  > had, so what? Why is it even worth commenting on?[/color]

                  Well - obviously its not worth commenting on your posts.

                  Diez

                  Comment

                  • Steve Holden

                    #10
                    Re: implementation of "complex&q uot; type

                    Russ wrote:[color=blue]
                    > "When you are the one seeking help, you may find it helpful to be
                    > polite.... I have found that comp.lang.pytho n has some of the most
                    > friendly and helpful people around. They will undoubtably help you (as
                    > they helped me many times), if you provide answers to the questions
                    > they ask in trying to help you... "
                    >
                    > Asking sarcastically why I am "reinventin g the wheel" is a funny way of
                    > being friendly. I said absolutely nothing to indicate that I had
                    > re-implemented my own version of complex. And, by the way, even if I
                    > had, so what? Why is it even worth commenting on?
                    >[/color]
                    You wrote, in message
                    <1141895757.128 437.159460@i39g 2000cwa.googleg roups.com>:
                    [color=blue]
                    > But when I tried the same thing with my own class in place of
                    > "complex" above, I found that both x and y were doubled.[/color]

                    So while you may not have implemented your own version of complex you
                    clearly implemented something you wanted to behave in the same way - and
                    that didn't.

                    Don't worry, let's not bother about your real problem, let's just go on
                    arguing about what you said or didn't say.

                    Better still, let's not bother ... I'm sorry you don't like the support
                    around here, I guess you get what you ask for.

                    regards
                    Steve
                    --
                    Steve Holden +44 150 684 7255 +1 800 494 3119
                    Holden Web LLC/Ltd www.holdenweb.com
                    Love me, love my blog holdenweb.blogs pot.com

                    Comment

                    • Russ

                      #11
                      Re: implementation of &quot;complex&q uot; type

                      "Better still, let's not bother ... I'm sorry you don't like the
                      support
                      around here, I guess you get what you ask for. "

                      Boy, some people are very sensitive around here. And some of them also
                      have a bad habit of misrepresenting my views.

                      For the record, I am very happy with the support here. In fact, I
                      solved the problem thanks to a couple of helpful replies to my query,
                      and I certainly appreciate that help.

                      What I don't appreciate is having people leap to unwarranted
                      assumptions and falsely claim that I don't appreciate the help I get. I
                      can do without that, thanks.

                      Comment

                      • Steven D'Aprano

                        #12
                        Re: implementation of &quot;complex&q uot; type

                        On Thu, 09 Mar 2006 09:20:17 -0800, Russ wrote:
                        [color=blue]
                        > "Why don't you show us your complex class?"
                        >
                        > Because I don't have a complex class. I merely used the complex class
                        > as an example to test the referencing behavior. Please read more
                        > carefully next time.[/color]

                        Or why don't you explain yourself more clearly next time? You post was
                        unclear, and I was lead to believe that you were trying to duplicate the
                        complex class.

                        In any case, it doesn't take a genius to recognise that the question is
                        still a valid question if you mentally remove "complex" from the sentence.
                        Why don't you show us your class, whatever that class may be? At least the
                        relevant methods. It may help us to spot the error if we can actually see
                        the error...

                        As for your suggestion in a later post that I was asking sarcastically why
                        you were re-inventing the wheel, there was no sarcasm intended. I think
                        you are being overly-sensitive, perhaps because you realise that your
                        question really wasn't a good one. There *are* good reasons for
                        re-inventing the wheel, but there were also bad reasons, and given that I
                        was under the mistaken impression that you had re-created the complex
                        class, it was a perfectly legitimate question to ask why. You had a
                        problem to solve, and were asking for help on solving the problem. To give
                        *effective* help, we need to understand what you are trying to do.

                        Otherwise we mistakenly think you are trying to implement your own complex
                        class *wink*


                        --
                        Steven.

                        Comment

                        • Russ

                          #13
                          Re: implementation of &quot;complex&q uot; type

                          Thanks for the links, especially for the pure Python implementation.
                          That provides a good model for similar classes.

                          I am just wondering why your implementation of complex numbers does not
                          have "assignment operators" such as "__iadd", etc.

                          By the way, I suppose my original post (where I wrote, "I'd like to
                          make my class behave like the "complex" class.") could be construed to
                          mean that I was implementing my own version of the complex class. But I
                          only meant that I want it to "behave" like the built-in complex class
                          with regard to referencing and copying (as my code example showed). My
                          apologies for any misunderstandin g that occurred.

                          Comment

                          Working...