Menu

mutable or immutable? your vote!

2000-11-06
2000-12-18
  • Alex Martelli

    Alex Martelli - 2000-11-06

    Should object types exposed from gmpy be immutable (like builtin numbers) or mutable (for efficiency with += etc)? I'm open to suggestions & arguments on both sides -- fire away!

     
    • Fred L. Drake, Jr.

      Immutable.

      Numbers in Python have traditionally been treated as immutable objects.  While this imposes some performance constraints (new objects have to be allocated more often), this allows the expected value semantics.  Not surprising users is a good thing!

       
      • Paul Moore

        Paul Moore - 2000-12-18

        Agreed - immutable.

        But could you get effective immutability by copy-on-write, doing something like checking in the += code whether the ref count is 1. If it is, modify in place, otherwise copy.

        The only problem is that the resulting numbers can't be dictionary keys. Nasty.

        It may (just barely!) be worth having a mutable version if the performance issue is sufficiently critical. But even then, I would use copy-on-write as above. I don't think that under any circumstances

            a = MPZ(1)
            b = a
            a += 1
            print b

        should give any answer other than 1...

         
    • Fred L. Drake, Jr.

      Is there any reason to use C++?  You're binding a C library to a C-based language interpreter.  Where's the advantage in using C++?  It's less portable simply because many of the compilers still aren't up to snuff with all the language features on minority platforms, but most everyone seems to have an ANSI C compiler.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.