Note that there are some explanatory texts on larger screens.

plurals
  1. POPython list([]) and []
    text
    copied!<pre><code>from cs1graphics import * from math import sqrt numLinks = 50 restingLength = 20.0 totalSeparation = 630.0 elasticityConstant = 0.005 gravityConstant = 0.110 epsilon = 0.001 def combine(A,B,C=(0,0)): return (A[0] + B[0] + C[0], A[1] + B[1] + C[1]) def calcForce(A,B): dX = (B[0] - A[0]) dY = (B[1] - A[1]) distance = sqrt(dX*dX+dY*dY) if distance &gt; restingLength: stretch = distance - restingLength forceFactor = stretch * elasticityConstant else: forceFactor = 0 return (forceFactor * dX, forceFactor * dY) #return a tuple def drawChain(chainData, chainPath, theCanvas): for k in range(len(chainData)): chainPath.setPoint(Point(chainData[k][0], chainData[k][1]),k) theCanvas.refresh() #refresh canvas chain = [] #chain here for k in range(numLinks + 1): X = totalSeparation * k / numLinks chain.append( (X,0.0) ) paper = Canvas(totalSeparation, totalSeparation) paper.setAutoRefresh(False) curve = Path() for p in chain: curve.addPoint(Point(p[0], p[1])) paper.add(curve) graphicsCounter = 100 somethingMoved = True while somethingMoved: somethingMoved = False oldChain = list(chain) #oldChain here for k in range(1, numLinks): gravForce = (0, gravityConstant) leftForce = calcForce(oldChain[k], oldChain[k-1]) rightForce = calcForce(oldChain[k], oldChain[k+1]) adjust = combine(gravForce, leftForce, rightForce) if abs(adjust[0]) &gt; epsilon or abs(adjust[1]) &gt; epsilon: somethingMoved = True chain[k] = combine(oldChain[k], adjust) graphicsCounter -= 1 if graphicsCounter == 0: drawChain(chain, curve, paper) graphicsCounter = 100 curve.setBorderWidth(2) drawChain(chain, curve, paper) </code></pre> <p>I was told that <code>list([]) == []</code>. So why is this code doing<br> <code>oldChain = list(chain)</code> instead of <code>oldChain = chain</code></p> <p>it's the same thing so it does not matter either way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload