EGR 103/Fall 2023/Lab 12

From PrattWiki
Jump to navigation Jump to search

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 in 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.

Error Handling

The commands that operate between a vector and another item have some error handling code to make sure the other item is of the right type. For example:

def __mul__(self, other):
        if type(other).__name__ in ["float", "int"]:
            return Vector103(
                0, 
                0, 
                0
                ) # Fix this line
        else:
            raise TypeError("unsupported operand types for *: {} and {}".format(
                type(self).__name__, type(other).__name__))

checks to see if the "other" item is either a float or an int. If it is, then it will return a Vector103 object. If it is not, then Python will raise an error that says that the other item is the wrong type. As another example:

    def __add__(self, other):
        if type(other).__name__ in ["Vector103"]:
            return Vector103(
                0, 
                0, 
                0
                ) # Fix this line
        else:
            raise TypeError("unsupported operand types for +: {} and {}".format(
                type(self).__name__, type(other).__name__))

checks to make sure the other is a Vector103 type object.

References

  • Complex103 in Lec24 folder of the public archive
  • 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