[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math

Search:


View post   

>> No.9572930 [View]
File: 18 KB, 754x286, Screen Shot 2018-03-09 at 8.45.31 AM.png [View same] [iqdb] [saucenao] [google]
9572930

>>9572912
Two things:
1) Holy shit, what is this? a picture for ants?
2)Okay, so a way to solve these problems usually is to type it up yourself and experiment.

When I typed up the code, I wanted to make sure the behavior we see extends to more than just the two function calls you're referring to. I did this by calling the function a couple of more times with different arguments an seeing the result (see picture).

As you can see, for some reason, the "list" variable doesn't get reset between function calls.
Looking this up, we see here:
http://docs.python-guide.org/en/latest/writing/gotchas/
that not only is it expected behavior, it's encouraged in some situations.

This doesn't really have anything to do with heap allocation (python is too high level to worry about that, much to my annoyance), so basically the reason it does this is "it was designed that way" but if you want to be more specific, just say "default arguments are evaluated at the time a function is defined, not when you run the function"

>>9572879
Conservation of momentum:
[math]
v_1 m_1 = v_2 m_2
v_2 = v_1 \cdot \frac{m_1}{m_2}
v_2 = 2 \cdot \frac{1}{2}
v_2 = 1m/s
[/math]

Navigation
View posts[+24][+48][+96]