19
46
bterwijn

@programming.dev

bterwijn 6 points 20 days ago

The difference between a |= b and a = a | b is exactly the same as the difference between a += b and a = a + b. Nothing new about that, try:

a = [1]
b = a
b += [2]
b.append(3)
b = b + [4]
b.append(5)

print(a)

or see: https://fosstodon.org/@bterwijn/116328991093782469

For immutable types there is no difference between a += b and a = a + b, try with a tuple.

path: 0 26010747 26011278, hotness: undefined, score: 6, children: 12
bterwijn 5 points 18 days ago

If you are interested see the dictionary docs (https://docs.python.org/...):

d |= other Update the dictionary d with keys and values from other, which may be either a mapping or an iterable of key/value pairs. The values of other take priority when d and other share keys. Added in version 3.9.

Pick your answer and check the Solution link.

path: 0 26023432 26031481, hotness: undefined, score: 5, children: 0
bterwijn 5 points 6 months ago

It's definitely not the same. Similarly for a class you can define the __add__ dunder method for a + b and separately the __iadd__ dunder method for a += b. The first creates a new object, the latter changes/mutates the existing object a. For immutable types it is the same though.

path: 0 22985458 22987895, hotness: undefined, score: 5, children: 2
bterwijn 5 points a year ago

Yes I understand your point, but I'm trying to reach out so people are aware and can use it in Python education. I feel it can really help beginners understand tricky concepts with ease, bit it's hard to reach a bigger audience these days. Sorry for the repetition, I'll guess I should cut back a bit.

path: 0 20583825 20587459, hotness: undefined, score: 5, children: 0
bterwijn 5 points 20 days ago

This is what the solution link is supposed to give you at the end of the execution animation:

It's hard to support all mobile browsers, on some it does work.

path: 0 26010580 26010673, hotness: undefined, score: 5, children: 0
bterwijn 4 points a year ago

The "Solution" link gives the solution to the exercise, the "Explanation" link explains the Python data model concepts behind the exercise. If some parts are hard to understand let me know.

path: 0 19165423 19165834 19170371, hotness: undefined, score: 4, children: 0
bterwijn 4 points 19 days ago

Thanks, maybe I should have added that possible answer instead of 'D', adding one more feels too many.

path: 0 26017588 26018461, hotness: undefined, score: 4, children: 0
bterwijn 3 points 20 days ago path: 0 26010135 26010522, hotness: undefined, score: 3, children: 2
bterwijn 3 points 19 days ago

If you don't want mutability you have to go to a pure functional language like Haskell, but then you have to copy a big list every time you make a change. There are ways to optimize copying by secretly sharing data behind the scene but you pay a performance price in some way. Then again, Python is slow and has mutability but popular for other reasons.

path: 0 26010747 26011278 26017662 26018427, hotness: undefined, score: 3, children: 10
bterwijn 3 points a year ago path: 0 20097465 20099609, hotness: undefined, score: 3, children: 2
bterwijn 3 points a year ago

Thanks, glad it helps you.

path: 0 19166063 19169237, hotness: undefined, score: 3, children: 0
bterwijn 3 points a year ago

Yes, that is a surprise to many, in other languages 'x+=y' and 'x=x+y' are the same.

path: 0 19165423 19169963 19170279, hotness: undefined, score: 3, children: 0
bterwijn 3 points 19 days ago

I think the confusion comes from a += b being equivalent to a = a + b for immutable types, so some people generalize that incorrectly to mutable types too. Otherwise I think it's pretty clear a += b mutates a, and a = a + b first computes a + b and then reassigns that to a so that its identity changes, just like in c = a + b.

If you implement these operations in a class you have to implement each dunder, __iadd__(self, other) and __add__(self, other) separately, same thing in C++.

path: 0 26010747 26011278 26017662 26018427 26022792 26023028, hotness: undefined, score: 3, children: 4
bterwijn 3 points 19 days ago

I've built a nice visualizer for internalizing what is going on under the hood, I hope that can help people.

path: 0 26010747 26011278 26017662 26018427 26018751 26023082, hotness: undefined, score: 3, children: 2
bterwijn 3 points 3 months ago path: 0 24519574, hotness: undefined, score: 3, children: 0
bterwijn 3 points 18 days ago

There is one important difference between a |= b and a = a | b, that is one part that makes this exercise difficult. See the dictionary docs (https://docs.python.org/...):

d |= other Update the dictionary d with keys and values from other, which may be either a mapping or an iterable of key/value pairs. The values of other take priority when d and other share keys. Added in version 3.9.

Pick your answer and check the Solution link.

The zen of Python lies unless you're Dutch. I'm Dutch and when I look out of my office I see the CWI where Guido wrote the original Python versions back in the day, see "Python the documentory" on YouTube.

path: 0 26023432 26026424 26031410, hotness: undefined, score: 3, children: 2
bterwijn 2 points a year ago

Thanks for reporting, should be fixed now.

path: 0 19172363 19172598 19173889 19174228, hotness: undefined, score: 2, children: 0
bterwijn 2 points 20 days ago

Can you tell me what the problem is for you when you click the first link? I tried to make it work for all browsers (even mobile, but the UI isn't very mobile friendly), so I would like to get it working on your system too. If you can give me a bug report (and maybe try from different system), that would be great.

The second link explains:

It's left to the reader to generalize this to x |= y vs x = x | y.

path: 0 26008300 26010404, hotness: undefined, score: 2, children: 2
bterwijn 2 points a year ago

Thanks for your feedback, much appriciated.

I agree that an exercise14.rst would be nice, but to save time I've let the code speak for itself now together with the visualizaion. I'll probably revisit and better document the exercises later.

At the Explanation link I try to give a general explanation about Pyrhon mutability (and copy later on), I agree some readers might find it hard to relate that to a specific exercise, but I don't want to write a specific explanation for each exercise.

path: 0 19178073 19181113, hotness: undefined, score: 2, children: 4
bterwijn 2 points 20 days ago

Diagnostics came up with some "double percent-encoding" issue on IOS. I've updated the website with a workaround, hopefully it does work now. Thanks for bug reporting.

path: 0 26008300 26010404 26010473 26011096, hotness: undefined, score: 2, children: 0

thanks for using Leebra!

go to feed...