Difference between revisions of "EGR 103/Spring 2023/Lab 13"

From PrattWiki
Jump to navigation Jump to search
(Created page with "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...")
 
 
Line 1: Line 1:
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 in your program.
+
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==
 
==Notes==
Line 34: Line 34:
 
The <code>give_loan</code> method is called by the <code>me</code> variable, which means that the information in <code>me</code> will be stored in the local variable <code>self</code> and the information in <code>you</code> will be stored in the local variable <code>other</code>.
 
The <code>give_loan</code> method is called by the <code>me</code> variable, which means that the information in <code>me</code> will be stored in the local variable <code>self</code> and the information in <code>you</code> will be stored in the local variable <code>other</code>.
  
 +
== 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:
 +
 +
<syntaxhighlight lang=python>
 +
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__)) 
 +
 +
</syntaxhighlight>
 +
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:
 +
 +
<syntaxhighlight lang=python>
 +
    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__)) 
 +
</syntaxhighlight>
 +
checks to make sure the other is a Vector103 type object.
  
 
== References ==
 
== References ==
 
* [https://towardsdatascience.com/introduction-to-python-classes-da526ff745df 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.
 
* [https://towardsdatascience.com/introduction-to-python-classes-da526ff745df 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.
 
* [https://python-course.eu/oop/magic-methods.php Magic Methods] by Bernd Klein on python-course.eu.  This is a brief introduction to classes and dunder methods
 
* [https://python-course.eu/oop/magic-methods.php Magic Methods] by Bernd Klein on python-course.eu.  This is a brief introduction to classes and dunder methods

Latest revision as of 15:49, 13 April 2023

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

  • 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