β² 72 βΌ Python f-string quiz (fstrings.wtf) submitted 1 year ago by learnbyexample@programming.dev to c/python@programming.dev 15 comments fedilink hide all child comments
[β] Eheran@lemmy.world 7 points 1 year ago (6 children) Walrus operator. What did I just read? permalink fedilink source hideshow 6 child comments replies: [β] sugar_in_your_tea@sh.itjust.works 13 points 1 year ago (4 children) I love the walrus operator: if (x := some_function()): do_something(x) else: # x is None or False or something, consider it invalid The only thing I wish was different is adding a scope, which would make x invalid outside the block. But Python's scoping rules are too dumb to handle this case. permalink fedilink source parent hideshow 4 child comments replies: [β] joyjoy@lemmy.zip 6 points 1 year ago Walrus is more useful in a while loop. while (data := f.read(1024)): pass permalink fedilink source parent [β] tunetardis@piefed.ca 2 points 1 year ago I think my most common use case is with dictionary lookups. if (val := dct.get(key)) is not None: # do something with val I've also found some cases where the walrus is useful in something like a list comprehension. I suppose expanding on the above example, you you make one that looks up several keys in a dict and gives you their corresponding values where available. vals = [val for key in (key1, key2, key3) if (val := dct.get(key)) is not None] permalink fedilink source parent [β] Ephera@lemmy.ml 2 points 1 year ago (1 child) In lots of functional languages, you'd have some_function() return an Option type and then you'd use .map() or similar on it to only do something when the value is defined. I think, that can be done in Python's syntax, but you need a library for the Option type... permalink fedilink source parent hideshow 1 child comment replies: [β] sugar_in_your_tea@sh.itjust.works 3 points 1 year ago Python has an "Optional" type, but it's merely an alias for T | None. I wish Python had better support for FP, but each time it gets close, it doesn't quite go far enough. For example, it now has match blocks, but there's no error if the match is exhaustive, which hurts formal proofs of correctness. Likewise, tons of other features fall a bit short, but in general it's workable with some discipline. permalink fedilink source parent [β] kryptonianCodeMonkey@lemmy.world 2 points 1 year ago* (last edited 1 year ago) Walrus operator does an inline assignment to a variable and resolves to the value assigned. If it is in a condition statement, like "if x := y:", it assigns the value of y to x then interprets the expression of the condition as of it just said "if x:". Functionally, that means the assignment happens regardless of the value of y, but the condition only passes if the value of y is "truthy", i.e. if it's not None, an empty collection, numerically equal to zero, or just False. permalink fedilink source parent
[β] sugar_in_your_tea@sh.itjust.works 13 points 1 year ago (4 children) I love the walrus operator: if (x := some_function()): do_something(x) else: # x is None or False or something, consider it invalid The only thing I wish was different is adding a scope, which would make x invalid outside the block. But Python's scoping rules are too dumb to handle this case. permalink fedilink source parent hideshow 4 child comments replies: [β] joyjoy@lemmy.zip 6 points 1 year ago Walrus is more useful in a while loop. while (data := f.read(1024)): pass permalink fedilink source parent [β] tunetardis@piefed.ca 2 points 1 year ago I think my most common use case is with dictionary lookups. if (val := dct.get(key)) is not None: # do something with val I've also found some cases where the walrus is useful in something like a list comprehension. I suppose expanding on the above example, you you make one that looks up several keys in a dict and gives you their corresponding values where available. vals = [val for key in (key1, key2, key3) if (val := dct.get(key)) is not None] permalink fedilink source parent [β] Ephera@lemmy.ml 2 points 1 year ago (1 child) In lots of functional languages, you'd have some_function() return an Option type and then you'd use .map() or similar on it to only do something when the value is defined. I think, that can be done in Python's syntax, but you need a library for the Option type... permalink fedilink source parent hideshow 1 child comment replies: [β] sugar_in_your_tea@sh.itjust.works 3 points 1 year ago Python has an "Optional" type, but it's merely an alias for T | None. I wish Python had better support for FP, but each time it gets close, it doesn't quite go far enough. For example, it now has match blocks, but there's no error if the match is exhaustive, which hurts formal proofs of correctness. Likewise, tons of other features fall a bit short, but in general it's workable with some discipline. permalink fedilink source parent
[β] joyjoy@lemmy.zip 6 points 1 year ago Walrus is more useful in a while loop. while (data := f.read(1024)): pass permalink fedilink source parent
[β] tunetardis@piefed.ca 2 points 1 year ago I think my most common use case is with dictionary lookups. if (val := dct.get(key)) is not None: # do something with val I've also found some cases where the walrus is useful in something like a list comprehension. I suppose expanding on the above example, you you make one that looks up several keys in a dict and gives you their corresponding values where available. vals = [val for key in (key1, key2, key3) if (val := dct.get(key)) is not None] permalink fedilink source parent
[β] Ephera@lemmy.ml 2 points 1 year ago (1 child) In lots of functional languages, you'd have some_function() return an Option type and then you'd use .map() or similar on it to only do something when the value is defined. I think, that can be done in Python's syntax, but you need a library for the Option type... permalink fedilink source parent hideshow 1 child comment replies: [β] sugar_in_your_tea@sh.itjust.works 3 points 1 year ago Python has an "Optional" type, but it's merely an alias for T | None. I wish Python had better support for FP, but each time it gets close, it doesn't quite go far enough. For example, it now has match blocks, but there's no error if the match is exhaustive, which hurts formal proofs of correctness. Likewise, tons of other features fall a bit short, but in general it's workable with some discipline. permalink fedilink source parent
[β] sugar_in_your_tea@sh.itjust.works 3 points 1 year ago Python has an "Optional" type, but it's merely an alias for T | None. I wish Python had better support for FP, but each time it gets close, it doesn't quite go far enough. For example, it now has match blocks, but there's no error if the match is exhaustive, which hurts formal proofs of correctness. Likewise, tons of other features fall a bit short, but in general it's workable with some discipline. permalink fedilink source parent
[β] kryptonianCodeMonkey@lemmy.world 2 points 1 year ago* (last edited 1 year ago) Walrus operator does an inline assignment to a variable and resolves to the value assigned. If it is in a condition statement, like "if x := y:", it assigns the value of y to x then interprets the expression of the condition as of it just said "if x:". Functionally, that means the assignment happens regardless of the value of y, but the condition only passes if the value of y is "truthy", i.e. if it's not None, an empty collection, numerically equal to zero, or just False. permalink fedilink source parent