EGR 103/Spring 2022/Lab 12

From PrattWiki
Jump to navigation Jump to search

Not much to go here! The test cases in the starter code will help identify any issues you may have before uploading to Gradescope. You will definitely want to get the __repr__ method working first so you can actually see what your vectors look like by typing their name in the console or using print(a) or print('{}'.format(a)) in the console or your program.

Notes

  • When you call a method by appending it to a variable of that method type, the information the method needs from the variable will be stored in "self." For example, with the following code:
class customer:
    def __init__(self, myname="", bal=0, acct=0):
        self.name = myname
        self.balance = bal
        self.acctnumber = acct
        
    def __repr__(self):
        return "{} ({}): ${:0.2f}".format(self.name, 
                                     self.acctnumber,
                                     self.balance)
        
    def give_loan(self, other_person, amount):
        if amount > self.balance:
            print("Insufficient funds!")
        else:
            other_person.balance += amount
            self.balance -= amount


if __name__=="__main__":
    me = customer("Person 1", 1000)
    you = customer("Person 2", 1000)
    print(me, "\n", you, "\n")
    me.give_loan(you, 100)
    print(me, "\n", you, "\n")

The give_loan method is called by the me variable, which means that the information in me will be stored in the local variable self and the information in you will be stored in the local variable other.


References

  • Introduction to Python Classes by Soner Yıldırım on TowardsDataScience.com. This is a brief introduction to classes. We did not discuss the idea of "inheritance" and you will not need it for this assignment, but it is an interesting option with classes.
  • Magic Methods by Bernd Klein on python-course.eu. This is a brief introduction to classes and dunder methods