DEV Community

Cover image for What Did You Learn This Week --April 24?
Waylon Walker
Waylon Walker

Posted on

What Did You Learn This Week --April 24?

Top comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

This week I learned about binding default values to dynamically created lambdas in python.

## WRONG πŸ’₯
files = ['abc', 'def', 'ghi', 'jkl']
funcs = []
for file in files:
   funcs.append(lambda: print(file))
funcs[0]()

😲 This prints 'jkl'. in fact every function prints 'jkl', they just print the latest value of 'jkl'

## CORRECT πŸŽ‰
files = ['abc', 'def', 'ghi', 'jkl']
funcs = []
for file in files:
   funcs.append(lambda x=file: print(x))
funcs[0]()

Now this is more of what I was looking for. The value of file was bound to the lambda upon creation, and will print out each value of the list as intended... Hard lesson learned this week.

Collapse
 
matteobruni profile image
Matteo Bruni

This week was a productive one!

  • I improved last week TypeScript unit testing
  • Created particles emitters

  • Learnt about Spatial Hash Maps to improve performances of particles (w/ help of another contributor)
  • Fixed the collision system that was just random, now it has math 🀣

  • Created particles absorbers (black holes)

  • A lot of refactoring and bug fixing, but it's not much exciting 🀣
Collapse
 
waylonwalker profile image
Waylon Walker

Those particles are amazing.