13

A couple of my friends failed to answer the correct output for this.
I have been working on Python since a month, they have, for months and years.
I am so depressed right now.

Comments
  • 2
    Ok am bad at this, but isn't there an error in second loop? Since b is not a list in second loop?
  • 8
    [[1],...,[9]] twice

    But really it ain't that clear. At a quick glance I'd probably have said it would yield an undefined error for b.
  • 1
    I'm not a python dev but isn't the output the same? b = [i] would make b an array with index 0 = i, right? b[0] = i would also set index 0 = i, yes? I'm probably wrong though. 🤷‍♂️
  • 15
    And by "so depwessed" you mean happy and cocky, right...
  • 1
    @telephantasm no because of those friend is in final year of graduation, and I was gave her tips on what topics to study to get placed in a good company.
  • 3
    The output wouldn't be an error or [[1],...[9]] twice.

    Correct output is:
    1-9
    9.. 9 (9 times)
  • 0
  • 7
    Wow, I can write bad code that confuses people too. I thought I was the only one! We're so much better than those people who think they know what they're doing.
  • 10
    Everyone above is wrong. The actual output is: "how are you any productive with that much transparency on your terminal window?"
  • 2
    @BugsBuggy
    I ran this program in 3.7 shell and it gave the same answer as you wrote with brackets ( [[1]..[9]] and [9]...[9] )

    I guess its maybe because variables created inside the loop also have the same scope as the ones outside?
  • 1
    So this works because b is hoisted to the top for some reason. Removing the first for-loop breaks the whole thing. Also adding print(c) in the second loop gives you this (see image) which explains the output and somewhat weird(?) behavior.
  • 3
    Thanks for reminding me why I dislike python.
    Its lexical scoping is stupid.
  • 0
    Is it because each element of c is a reference to b? Which is constantly being changed and thus will reflect the final version of b?! Fuck!

    I have encountered this a million times, and Everytime I struggle to understand it at all for like 20 min!

    Maybe if this was in c and I could tell if it was a pointer this would make some goddamn sense!
  • 1
    @daegontaven yup
  • 1
  • 1
    @dontPanic I'm not good with linux terminal sometimee, so with transparency, I can see the commands from the docs I have opened in browser below the terminal :P
  • 1
    @everyone the reason that it depresses me is because everyone has been using the concept in some way or the other, like calling function on an object without assigning any return value to any variable. Eg:

    ```
    x=SomeRandomObject
    random_function(x) #changes values in x
    print x
    #TADA: x gets changed here as well
    ```
  • 0
    @BugsBuggy Right, it's by reference. God that's messy 😅
Add Comment