[Python-ideas] Way to repeat other than "for _ in range(x)"

Pavol Lisy pavol.lisy at gmail.com
Fri Mar 31 04:21:02 EDT 2017


On 3/31/17, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, Mar 30, 2017 at 04:23:05PM +0200, Pavol Lisy wrote:
>> On 3/30/17, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> > On 30 March 2017 at 19:18, Markus Meskanen <markusmeskanen at gmail.com>
>> > wrote:
>> >> Hi Pythonistas,
>> >>
>> >> yet again today I ended up writing:
>> >>
>> >> d = [[0] * 5 for _ in range(10)]
>>
>>     d = [[0]*5]*10  # what about this?
>
> That doesn't do what you want.
>
> It's actually a common "gotcha", since it makes ten repetitions of the
> same five element list, not ten *copies*.

Yes. It is clear that I did mistake here (sorry for that!).

>> Simplified repeating could be probably useful in interactive mode.

> I'm sorry, did you just suggest that language features should behave
> differently in interactive mode than non-interactive mode?

No. I did not suggest it. In contrary: if it is really useful for
interactive python (*) then I suggest to implement it generally!

(*) - this is matter of discussion where I don't think this proposal
will be accepted...

Python is multipurpose language which means that something which is
not useful in A may be useful in B.
Interactive python could be something like discussing with computer
where we don't like to talk too much to say a little.

> The point of interactive mode is to try
> out syntax and code and see what it does, before using it in non-
> interactive scripts. If things behave differently, people will be left
> confused why the *exact same line of code* works differently in a script
> and when they try it interactively.

Sorry, this is not true. Not everybody use interactive python only for
testing python constructs. (For example see
http://ipython.readthedocs.io/en/stable/interactive/shell.html?highlight=shell
)

PL


More information about the Python-ideas mailing list