[Python-ideas] Python-ideas Digest, Vol 141, Issue 145

James Lu jamtlu at gmail.com
Mon Aug 27 17:18:37 EDT 2018


> As Matthew points out, you could use numpy.array. Or code your own
> class, by providing __add__ and __iadd__ methods.
>
> >>> import numpy
> >>> a = numpy.array([1, 2])
> >>> b = numpy.array([3, 4])
> >>> a + b
> array([4, 6])
> >>> a += b
> >>> a
> array([4, 6])

I could, but I don't think that justifies not having this functionality in
python
standard. From the language experience perspective, numpy is often a
pain to install on most systems. If I'm designing card games and I
just want to run a quick monte carlo simulation, the experience should be
as smooth as possible.

This is something I think most students will expect while learning python,
especially if they're implementing algorithms.

On Mon, Aug 27, 2018 at 4:24 AM <python-ideas-request at python.org> wrote:

> Send Python-ideas mailing list submissions to
>         python-ideas at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/python-ideas
> or, via email, send a message with subject or body 'help' to
>         python-ideas-request at python.org
>
> You can reach the person managing the list at
>         python-ideas-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Python-ideas digest..."
>
>
> Today's Topics:
>
>    1. Re: Unpacking iterables for augmented assignment (Matthew Einhorn)
>    2. Re: Unpacking iterables for augmented assignment (Jonathan Fine)
>    3. Re: Pre-conditions and post-conditions (Jacco van Dorp)
>    4. Re: Pre-conditions and post-conditions (Ivan Levkivskyi)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 27 Aug 2018 01:29:14 -0400
> From: Matthew Einhorn <moiein2000 at gmail.com>
> To: python-ideas at python.org
> Subject: Re: [Python-ideas] Unpacking iterables for augmented
>         assignment
> Message-ID:
>         <CALCauYeAjPWek5L=BU-=
> yMM-FNYw3Bza2hJqsGmdgtrvBUa7XA at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> On Sun, Aug 26, 2018, 9:24 PM James Lu <jamtlu at gmail.com> wrote:
>
> > Hi Johnathan
> >
> > I echo your points. Indeed, the PEP referenced to refers to a "tuple
> > expression" in the grammatical and not the programmatic sense.
> >
> > Finally, here's something that surprised me a little bit
> >
> > >>> x = [1, 2]; id(x)
> > 140161160364616
> > >>> x += [3, 4]; id(x)
> > 140161160364616
> >
> > >>> x = (1, 2); id(x)
> > 140161159928520
> > >>> x += (3, 4); id(x)
> > 140161225906440
> >
> > Notice that '+=' creates uses the same object when the object is
> > a
> > list, but creates a new object. This raises the question: Why and
> > how
> > does Python behave in this way?
> >
> > It's because lists are mutable are tuples are immutable.
> > There's a dunder iadd method and a dunder add method.
> > iadd magic methods, operating on the left hand side, return None and
> > modify the object in-place. add magic methods return the result and
> > don't modify the object it's called on.
> > iadd is mutable add, whereas add is "return a copy with the result
> > added"
> >
> > >>> tuple.__iadd__
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > AttributeError: type object 'tuple' has no attribute '__iadd__'
> > type object 'tuple' has no attribute '__iadd__'
> > >>> tuple.__add__
> > <slot wrapper '__add__' of 'tuple' objects>
> > >>> list.__iadd__
> > <slot wrapper '__iadd__' of 'list' objects>
> > >>> list.__add__
> > <slot wrapper '__add__' of 'list' objects>
> >
> >
> > tuple1 = tuple1.__add__(tuple2)
> >
> > list1.__iadd__(list2)
> >
> > > Does it IN PRACTICE bring sufficient benefits to users?
> >
> > I found myself needing this when I was writing a monte-carlo
> > simulation in python that required incrementing a tallying counter
> > from a subroutine.
> >
>
>
> Wouldn't a numpy array be very suited for this kind of task?
>
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/python-ideas/attachments/20180827/a1c698af/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 2
> Date: Mon, 27 Aug 2018 07:25:00 +0100
> From: Jonathan Fine <jfine2358 at gmail.com>
> To: python-ideas <python-ideas at python.org>
> Subject: Re: [Python-ideas] Unpacking iterables for augmented
>         assignment
> Message-ID:
>         <CALD=
> Yf9B7ErXAnBvBXZ2yBjR4ZLeyv1tpHV7+U3ebO2sw08wPA at mail.gmail.com>
> Content-Type: text/plain; charset="UTF-8"
>
> James has suggested that Python be enhanced so that
> >>> a, b += c, d
> is a short-hand for
> >>> a += c
> >>> b += d
>
> Myself, James and Matthew wrote
>
> >> > Does it IN PRACTICE bring sufficient benefits to users?
>
> >> I found myself needing this when I was writing a monte-carlo
> >> simulation in python that required incrementing a tallying counter
> >> from a subroutine.
>
> > Wouldn't a numpy array be very suited for this kind of task?
>
> Perhaps, James, you might like to refactor your code so that
> >>> tally += simulation(args)
> does what you want.
>
> As Matthew points out, you could use numpy.array. Or code your own
> class, by providing __add__ and __iadd__ methods.
>
> >>> import numpy
> >>> a = numpy.array([1, 2])
> >>> b = numpy.array([3, 4])
> >>> a + b
> array([4, 6])
> >>> a += b
> >>> a
> array([4, 6])
>
> --
> Jonathan
>
>
> ------------------------------
>
> Message: 3
> Date: Mon, 27 Aug 2018 09:04:21 +0200
> From: Jacco van Dorp <j.van.dorp at deonet.nl>
> To: python-ideas <python-ideas at python.org>
> Subject: Re: [Python-ideas] Pre-conditions and post-conditions
> Message-ID:
>         <CAKnNRP=
> KZLVcH+6mK+imzvnCDf412QKpo4FOjmETLF2Euu9wgA at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Total noob speaking here, but....
>
> Those contracts are mostly important during development right ? Slowdown
> isn't that much of an issue during development. So you could make a debug
> mode that enforces the contracts, and a production mode that code users can
> use during production to stop the slowdown - in this case, your decorators
> can return their functions/classes unaltered. If they do end up with
> problems, you can always ask them to run the same inputs with debug mode on
> to see where it goes wrong.
>
> For the rest, i'm totally new to design by contract. I do get the draw of
> it, but im not sure if I'd ever really use it. I tend to solve my problems
> with a royal sprinkling of logger.debug(f"{val_i_need_to_check}").
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/python-ideas/attachments/20180827/e59ffdd2/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 4
> Date: Mon, 27 Aug 2018 09:24:20 +0100
> From: Ivan Levkivskyi <levkivskyi at gmail.com>
> To: j.van.dorp at deonet.nl
> Cc: python-ideas <python-ideas at python.org>
> Subject: Re: [Python-ideas] Pre-conditions and post-conditions
> Message-ID:
>         <CAOMjWk=ZrQSFBYKWGgne+2RgF8KvfJrRSt3qw=
> vNe9yHGFYjLg at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> TBH, I think one of the main points of design by contract is that contracts
> are verified statically.
> For runtime contract checking I would rather recommend hypothesis library
> (or similar).
>
> --
> Ivan
>
>
>
> On Mon, 27 Aug 2018 at 08:05, Jacco van Dorp <j.van.dorp at deonet.nl> wrote:
>
> > Total noob speaking here, but....
> >
> > Those contracts are mostly important during development right ? Slowdown
> > isn't that much of an issue during development. So you could make a debug
> > mode that enforces the contracts, and a production mode that code users
> can
> > use during production to stop the slowdown - in this case, your
> decorators
> > can return their functions/classes unaltered. If they do end up with
> > problems, you can always ask them to run the same inputs with debug mode
> on
> > to see where it goes wrong.
> >
> > For the rest, i'm totally new to design by contract. I do get the draw of
> > it, but im not sure if I'd ever really use it. I tend to solve my
> problems
> > with a royal sprinkling of logger.debug(f"{val_i_need_to_check}").
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org
> > https://mail.python.org/mailman/listinfo/python-ideas
> > Code of Conduct: http://python.org/psf/codeofconduct/
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/python-ideas/attachments/20180827/5cdeae6c/attachment.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
>
> ------------------------------
>
> End of Python-ideas Digest, Vol 141, Issue 145
> **********************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180827/ecec5ca7/attachment-0001.html>


More information about the Python-ideas mailing list