Static Methods #
In this lab you will learn about static methods through a Currency Conversion System.
Syllabus Topics [SL] #
- B3.1.3 Distinguish between static and non-static variables and methods.
Key Vocabulary #
| Word | Definition |
|---|---|
| Static Method | A method that belong to the class, not the individual objects. |
| @staticmethod decorator | Informs Python that the method is static. |
[0] Class Overview #
In this lab, you will experience a class that only has static methods and static attributes: CurrencyConverter.
CurrencyConversion has only static methods and static attributes. It’s helpful to have the functions grouped together in a class, but it does not require objects with unique attributes or methods.
Static methods can be called without can object, simply on the class itself.
class CurrencyConverter:
__CURRENCIES = ["HKD", "USD", "JPY", "EUR", "GBP", "THB", "KRW"]
__RATES = [1.0, 0.13, 20.32, 0.11, 0.096, 4.02, 188.68]
@staticmethod
def get_rate(currency):
"""Returns the rate for the currency"""
index = CurrencyConverter.__CURRENCIES.index(currency)
return CurrencyConverter.__RATES[index]
CurrencyConverter.get_rate("HKD")
๐ Here is the UML diagram for the class. The underline of an attribute or method denotes it is static.
classDiagram
class CurrencyConverter {
- CURRENCIES : string[]$
- RATES : float[]$
- SYMBOLS: string[]$
+ get_rate(string) float $
+ set_rate(string, float) None$
+ convert(float, string, string) float$
+ format(float, string) string$
+ parse_and_convert(string) string$
}
[1] Set up #
๐ป Clone your repo in the correct folder.
Be sure to replace yourgithubusername with your actual username.
cd ~/desktop/dpcs/unit03_oop
git clone https://github.com/isf-dp-cs/lab_static_methods_yourgithubusername
cd lab_static_methods_yourgithubusername
๐ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
๐พ ๐ฌ Exiting the poetry shellWhen you want to exit the shell, you can type
exitor^D
[2] Implement the methods #
๐ป Firstly, test using the existing static attributes and methods in the CurrencyConverter class.
๐ป Secondly, implement the following methods as outlined in the DOCSTRINGS
convert(amount, from_currency, to_currency)format(amount, currency)parse_and_convert(user_input)
๐ธ Currency Conversion Tips
- HKD to another currency: multiply by the rate
- another currency to another currency: convert the first currency to HKD then to the other currency
# HKD $10 -> USD $1.3
10 * 0.13 = 1.3
# JYP ยฅ1000 -> GDP ยฃ8.64
1000/20.32 -> 49.20 #1) convert JYP to HKD
4.92*0.096 = 4.72 # 2) convert HKD to GDP
[3] Deliverables #
โกโจ Once you finish the lab, be sure to complete these two steps:๐ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.
๐ป Push your work to Github
- git status
- git add -A
- git status
- git commit -m “describe your code here”
- git push
- remote