folkerschamel,
@folkerschamel@mastodon.social avatar

⁉️ Another for
!

What is the value of the following expression? Why?

sum(map(lambda q, x=[]: (x, x.append(1))[0], range(10)), [])

(Another riddle is https://mastodon.social/@folkerschamel/110876661322264891)

JoeP,
@JoeP@mastodon.world avatar

@folkerschamel
I executed it and was duly surprised. Leaving things vague for others who might want to guess, but...
When map() completes it returns a generator that has 10 copies of x. Which are lists / is a list.
Then sum() does what + does to lists.

JoeP,
@JoeP@mastodon.world avatar

@folkerschamel
Is it...a Stack Overflow post explaining why you shouldn't use mutable default values in lambdas, and ideally not anywhere?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • anitta
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • provamag3
  • tester
  • Leos
  • megavids
  • JUstTest
  • All magazines