# Code for Monte Carlo class, Jonathan Goodman # http://www.math.nyu.edu/faculty/goodman/teaching/MonteCarlo20/ # ObjectBinding.py # The author gives permission for anyone to use this publicly posted # code for any purpose. The code was written for teaching, not research # or commercial use. It has not been tested thoroughly and probably has # serious bugs. Results may be inaccurate, incorrect, or just wrong. # illustrate objects and binding in Python import numpy as np # to illustrate mutable objects print("\n Illustrate binding and mutation in Python\n") x = np.zeros(3) # numpy array is mutable z = np.zeros(3) # identical but not the same y = x # new name bound to the same object print("(x is y) is " + str(x is y)) # "is": bound to the same object? print("(x is z) is " + str(x is z)) print("(x == z) is " + str(x == z)) # "==": have the same value, element by element print("(x[1] is x[2]) is " + str(x[1] is x[2])) # different objects ... print("(x[1] == x[2]) is " + str(x[1] == x[2])) # ... can have the same value x[0] = np.pi OutFormat = "x[0] is {x0:6.4f}, y[0] is {y0:6.4f}, z[0] is {z0:6.4f}" Outline = OutFormat.format(x0 = x[0], y0 = y[0], z0 = z[0]) print(Outline) u = x.copy() x[1] = np.e u[0] = np.sqrt(2) OutFormat = "x is [{x0:6.4f}, {x1:6.4f}, {x2:6.4f}]" Outline = OutFormat.format(x0 = x[0], x1 = x[1], x2 = x[2]) print(Outline) OutFormat = "u is [{u0:6.4f}, {u1:6.4f}, {u2:6.4f}]" Outline = OutFormat.format(u0 = u[0], u1 = u[1], u2 = u[2]) print(Outline)