Difference between revisions of "EGR 103/Spring 2022/Lab 12"
Jump to navigation
Jump to search
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 your program. | 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: | ||
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | 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") | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | 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>. | ||
+ | |||
== 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 17:37, 13 April 2022
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