# Demonstration code for Scientific Computing class, # http://www.math.nyu.edu/faculty/goodman/teaching/SciComp2022/ # ClassDemo.py # uses Python 3 and Numpy # Python Classes Demo import BlankSlate as bs # A class definition with no content import GraySlate as gs # A minimal class definition import Helpers as h # Store routines here to make the demo readible # Create an instance of the class with nothing. # See what's in it by printing its namelist # Add something to it and demonstrate that the thing you added is there BlankInstance = bs.BlankSlate() # An instance of the class with nothing print("\nHere are all the names in the BlankInstance namespace.\n") for name in dir(BlankInstance): print(" " + name) print("\nHere are all the names that are not __***__.\n") myNames = h.myNames(dir(BlankInstance)) for name in myNames: print(" " + name) BlankInstance.printSomething = h.printHello print("\nHere are all the names that are not __***__") print("after adding one name\n") myNames = h.myNames(dir(BlankInstance)) for name in myNames: print(" " + name) print("\n When you call the function you added\n") BlankInstance.printSomething() # Create an instance of the gray class with data and a function. # Access the data and function, manipulate the data # Demonstrate the "names point to objects" principle GI3 = gs.GraySlate(3) print("\nThe non __***__ names in an instance of the GraySlate class.") print("These are put in by the constructor\n") myNames = h.myNames(dir(GI3)) for name in myNames: print(" " + name) nString = "\nGI3 has data member n ={n:2d}" nString = nString.format( n = GI3.n ) print(nString) x = 4 addString = "The add_n function with argument {x:2d} gives {a:2d}" addString = addString.format( x = x, a = GI3.add_n(x) ) print(addString) newGI = GI3 newGI.n = 7 print("Now, GI3 has n = {n:2d}".format(n=GI3.n))