BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Does the Python syntax a = [0] * n; mean the same as Perl: @a = (0) x $n;?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re: [OT] Python to Perl.
by davido (Cardinal) on Jun 15, 2018 at 15:06 UTC

    I'm sure you've done this:

    >>> a = [0] * 10 >>> a [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    ...and so are aware that, at least superficially, the construct is the same as @a = (0) x 10. So I imagine the question is more about whether below the surface they are similar.

    There is this gotcha:

    >>> a = [1,2] >>> b = [a] * 5 >>> a [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]] >>> a[0] = 2 >>> b [[2, 2], [2, 2], [2, 2], [2, 2], [2, 2]]

    But that's really no different than this:

    my @a = (1, 2); my @b = (\@a) x 5; $a[0] = 2; print Dumper \@b;

    It seems from documentation and behavior that the two are as close to being literal translations as can be hoped for across different languages.


    Dave

      I'm sure you've done this:

      Actually no. I don't currently have a working Python install.

      I did one time for the purpose trying things when converting python code, but then I installed something that brought its python installation with it, and it screwed my original install up. As I don't use the language, I've never bothered to try and sort it out.

      It seems from documentation and behavior that the two are as close to being literal translations as can be hoped for across different languages.

      Thanks. That pretty much what I got from the context, but as the overall conversion doesn't work quite right, I'm now looking piece by piece.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re: [OT] Python to Perl.
by Eily (Monsignor) on Jun 15, 2018 at 15:40 UTC

    The = sign in python doesn't change the value of the LHS (well, at least as long as you don't consider object attributes) but binds or rebinds the name, so that would be more like glob operations in perl. So b = a might be interpreted as *b = *a. Then all references to @a or $a (python lists act like arrayrefs otherwise, so working with scalars might be closer) will lose the connection, meaning the assignment won't have any effect on any other structure in the code. In perl, with @a = (0,) x $n; or $a = [ (0,) x $n ];, you might have a reference to @a or $a somewhere. So I'd say python's a = [0] * n is perl's *a = \[ (@{ [0] }) x $n ];

    Python variables don't act like either perl package variables or lexicals though, but rather like you had a global symbols table, and a an additionnal symbols table lexically bound to each function (so all variables in a function are visible everywhere in that function) that could hide some of the symbols of the global table.

      Thankfully, the subtleties of python alaising don't matter for this code.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re: [OT] Python to Perl.
by Marshall (Canon) on Jun 15, 2018 at 16:07 UTC
    No. (as you have already found out)

    I don't know how to do this seemingly simple task in Python without an executable loop.

    If you figure out a way to do this, please let me know.

      Actually yes. (Or whatever differences tehre are do not matter to the code I am converting as it now works.)

      It was another part of the code that was at fault; a range( 1 .. k ) that I had converted to 1 .. $k instead of 1..$k-1.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

        Ah, this makes sense at some wacky level.

        Consider this array (language doesn't matter here, so this is pseudo-code):

        array = ('a', 'b', 'c', 'd', 'e')

        Some languages use a convention where begin(array) is equivalent to array[0], and end(array) is equivalent to array[5], or one past the last element. A loop in such a world might be written like this:

        for (x = 0; x != end(array); ++x) do_something(array, x);

        The notation used for such a concept is sometimes this:

        [0,5)

        ...meaning (0 <= x < 5). This concept and notation is common in C++. And is used by Python's range() function.

        Python's range method has the following semantics:

        range(stop) # 0 .. stop-1 range(start,stop) # start .. stop-1 range(start,stop,step) # start .. stop-1 by step: for(ix=0; ix != sto +p; ix += step) {...}

        In each case start is inclusive, and stop is exclusive. [start,stop) in mathematical notation, or this: (start <= x < stop).


        Dave