how is everyone just fine with this. fruits = ['apple', 'orange', 'banana']

how is everyone just fine with this

fruits = ['apple', 'orange', 'banana']
fruits_d = dict.fromkeys(fruits, {})
print(fruits_d)

{'apple': {}, 'orange': {}, 'banana': {}}

fruits_d['apple']['flavor'] = ''
{'apple': {'flavor': ''}, 'orange': {'flavor': ''}, 'banana': {'flavor': ''}}

print(fruits_d)

** Process exited - Return Code: 0 **
Press Enter to exit terminal

CRIME Shirt $21.68

Nothing Ever Happens Shirt $21.68

CRIME Shirt $21.68

  1. 1 month ago
    Anonymous

    std::cout >> "Jerry fricking broke it." >> std::endl

  2. 1 month ago
    Anonymous

    sorry, I can't read that. Can you write it again in a real language like ASM?

    • 1 month ago
      Anonymous

      mov ecx, [fruits_d]
      mov eax, [ecx].flavor
      mov BYTE [eax], 0
      mov eax, [ecx+8].flavor
      mov BYTE [eax], 0
      mov eax, [ecx+16].flavor
      mov BYTE [eax], 0
      ret

      There ya go, friend. Fully optimized ASM code to set all the 'flavor' strings to empty. No need for that silly Python nonsense!

    • 1 month ago
      Anonymous

      Which ASM?

  3. 1 month ago
    Anonymous

    tell me how you would fix it

    • 1 month ago
      Anonymous

      that would require understanding why this is, which i do not

      • 1 month ago
        Anonymous

        >dict.fromkeys(fruits, {})
        Makes a dict using the list as keys and assigning the given value to them. The value is a dictionary you created in the function call and it uses that same dictionary as the value for every single key.

        • 1 month ago
          Anonymous

          Because that's exactly what's supposed to happen.
          Think of it this way:
          fruits = ['apple', 'orange', 'banana']
          somedict = {}
          dict.fromkeys(fruits, somedict)

          What you're doing is the same thing except you're not assigning the dict given as value to anything before you make the fromkeys call. But it behaves the same way as this piece of code.

          damn...thank you gentlemen

  4. 1 month ago
    Anonymous

    >ok first of all thats not the pythonic way of doing things. you should be using, you know, a dict comprehension.

    • 1 month ago
      Anonymous

      Who are you quoting?

    • 1 month ago
      Anonymous

      fruits_d = {fruit : {} for fruit in fruits}

      You just need to be more Pythonic™

      this

  5. 1 month ago
    Anonymous

    def foo(bar = None):
    if bar is None:
    bar = {}

  6. 1 month ago
    Anonymous

    Because that's exactly what's supposed to happen.
    Think of it this way:
    fruits = ['apple', 'orange', 'banana']
    somedict = {}
    dict.fromkeys(fruits, somedict)

    What you're doing is the same thing except you're not assigning the dict given as value to anything before you make the fromkeys call. But it behaves the same way as this piece of code.

  7. 1 month ago
    Anonymous

    fruits_d = {fruit : {} for fruit in fruits}

    You just need to be more Pythonic™

  8. 1 month ago
    Anonymous

    this is just the basic semantics of the language tho? dictis and other objects are refcounted, so you are not really creating 3 distinct empty dicts, you are creating 1 dict and referencing it 3 times. So when you change it through any reference, the changes are reflected everywhere else. Pretty normal for a interpreted language tbh, you can probably recreate this easily on JS or Lua and such.

    • 1 month ago
      Anonymous

      >Pretty normal for a interpreted language
      Copy-on-write is a sane thing sometimes.
      But never in Python, and that can be counterintuitive when people are forced to deal with when objects are actually made.
      I qualify as a Python hater.

  9. 1 month ago
    Anonymous

    dicts aren't a literal type, so {} isn't going to be copied as a value for each key, it'll be a reference to the dict you created instead

  10. 1 month ago
    Anonymous

    use Data::Dumper::Perltidy;

    $hash->{$_}->{flavor}->{python}->{sucks} = {} for (qw(apple orange banana));
    print Dumper $hash;

    $VAR1 = {
    'banana' => { 'flavor' => { 'python' => { 'sucks' => {} } } },
    'orange' => { 'flavor' => { 'python' => { 'sucks' => {} } } },
    'apple' => { 'flavor' => { 'python' => { 'sucks' => {} } } }
    };

  11. 1 month ago
    Anonymous

    So what's the use case where you'd want every key in your dict to point to the same mutable object? It's the same thing as function argument default values. It's counterintuitive to me that an interpreted language would store default instances instead of interpreting the code each time, but that's just a design choice. I still don't understand the use case.

Your email address will not be published. Required fields are marked *