# Author: James Tam # Version: November 12, 2024 # Learning concepts: understanding the need for self when inside the methods # of a class. class Person: def __init__(self,aName): self.name = aName #Stylistic benefit of self. #Clarifying whose method is being called. def sayFriend(self,myFriend): print("Calling object's name is %s" %(self.name), end=",\t") print("name of %s's friend is %s" %(self.name,myFriend.name)) def canSay(self): print("My name is %s" %(self.name)) #JT's hint for figuring this out, remember/review my scoping rules frome #the functional/decompostion section. # #When referring to an identifier: #1) Check for the creation of that idenifier in the local scope #2) If nothing local then check for the creation of the identifier globally. #Also keep in mind that methods are like functions in that they have local #memory that is hidden from other function/methods. In this case stacey and #jamie objects are created in start() def cannotSay(self): print("My name is %s" %(name)) def start(): stacey = Person("Stacey") jamie = Person("Jamie") print("Part I") print("Calling %s's sayFriend() method" %(stacey.name)) stacey.sayFriend(jamie) print("Calling %s's sayFriend() method" %(jamie.name)) jamie.sayFriend(stacey) print("What would the output be? Why?") jamie.sayFriend(jamie) print("\n--\nPart II") print() print("What will happen when this method is called? Why?") stacey.canSay() print() print("What will happen when this method (currently commented out) is called? Why?") #stacey.cannotSay() start()