string representations of objects oftenit is useful to have meaningful string representation of an object if we tell python to print an objectfor instance print(annesacc)python gives the cryptic answer "insteadwe would rather have string representation that really tells us what' going on with the attributes of the objectsuch as the information that the print info(selfmethod gives us in pythonwe can simply add an instance method that has the name str (self(no parameters besides self!to the class this method must return some string that describes the object we can see such method in the code listing on the following page in some casespython automatically realizes that we want string representation of an objectfor instance when calling print(annesaccor str(annesacc)it automatically calls the str (selfmethod of this object when calling print(annesacc)it prints out whatever the str (selfmethod of the annesacc object returnsstr(annesaccreturns the string that is returned by the method call annesacc str ( class accountmethods def __str__(self)res "**account info ***\nres +"account id:str(self number"\nres +"holder:self holder "\nres +"balancestr(self balance"\nreturn res if __name__ ="__main__"annesacc account( "anne"annesacc deposit( print(annesacchooks are special functions of class that are called automatically by python in some circumstances the str (selfmethod is only one example for this there is another hook method called repr (selfwhich is also supposed to return string that describes the object str (selfis supposed to return user-friendly description of the objectwhile repr (selfshould return description that is useful for developers note that print(anobjectreturns the string returned by the str (selfmethodwhile printing an object by just typing it in the interactive mode returns the string returned by repr (selfanother hook function that we have seen already is the constructor method init (selfargs))which is called when creating new object by calling class there are also hook methods that are triggered when operators are appliedfor example the operator triggers the add method of an object note that the names of hook methods usually start and end with two underscores we will learn more about hooks later annemarie friedrichcis lmu munchenws /