📝 Class Materials:
'''
To construct account objects from the Account class below I need a constructor which in Python is the __init__ magic method/dunder:
'''
class Account:
# Magic method/dunder __init__
def __init__(self, owner, amount):
self.owner = owner
self.amount = amount
self._transactions = []
'''
The constructor takes care of setting up the object. This allows us to create new accounts like this:
'''
joi_acc = Account('Joi Anderson', 1000)
print(joi_acc)
# Try these
print(joi_acc.__str__())
print(joi_acc.__repr__())
#lets import the datetime module
import datetime
#lets get the current date and time
# Notice how datetime displays __str__ and __repr__ differently!
now = datetime.datetime.now()
print('Using str: ' + now.__str__())
print('Using repr: ' + now.__repr__())
Define __str__
and __repr__
for this class:
'''
To construct account objects from the Account class below I need a constructor which in Python is the __init__ magic method/dunder:
'''
class Account:
# Magic method/dunder __init__
def __init__(self, owner, amount):
self.owner = owner
self.amount = amount
self._transactions = []
# Define __str__ for this class
def __str__(self):
return f'Bank Account owner {self.owner} and amount {self.amount}'
# Define __repr__ for this class
def __repr__(self):
return f'Account(\'{self.owner}\', {self.amount})'
'''
The constructor takes care of setting up the object. This allows us to create new accounts like this:
'''
joi_acc = Account('Joi Anderson', 1000)
# Notice here python prints __str__
print(joi_acc)
# Try these
print(joi_acc.__str__())
print(joi_acc.__repr__())
# main.py
from Animal import Animal
otter = Animal("Octavious")
print(str(otter))
print(repr(otter))
another_otter = Animal("Marlene")
new_otter = otter + another_otter
del new_otter
print(Animal.animal_count)
#print(new_otter)
class Animal:
"""This class represents an animal with a name"""
animal_count = 0
def __init__(self, name):
"""This method initializes the name property"""
self._name = name
Animal.animal_count += 1
def __str__(self):
"""Override the str magic method to show the name"""
return f"I'm an Animal and my name is {self._name} (Animals so far: {Animal.animal_count})"
def __repr__(self):
"""Override the repsr magic method to display a message to devs"""
return f"Animal({self._name})"
def __add__(self, other_animal):
"""Override the add magic method to create a new animal object and give it a merged name"""
new_name = f"{self._name} {other_animal._name}"
return Animal(new_name)
def __del__(self):
Animal.animal_count -= 1
# See source code for "str" object
# print(help(str))
# See source code for "int" object
#print(help(int))
# See source code for "float" object
#print(help(float))
# See source code for "list" object
#print(help(list))
# See source code for "dict" object
#print(help(dict))
# See source code for "tuple" object
#print(help(tuple))
# See source code for "set" object
#print(help(set))
# main.py
from IntFloatList import IntFloatList
intfloat = IntFloatList([2,4,8])
print(intfloat)
intfloat.append(10)
print(intfloat)
intfloat.append("hello")
print(intfloat)
# instructions.py
'''
Lets build a custom list class that will only accept integers and floats.
If we attempt to append, any other data-type, lets say for arguments sake a string, we will print an error message, to inform the user that this particular list can only accept integers and floats.
'''
# TODO: Create a IntFloatList.py and create a IntFloatList class that inherits from list
# TODO: import IntFloatList into main.py
# TODO: Create an object of type IntFloatList and call it intfloat_list. What happens when it is printed?
# TODO: To add elements to our list, we pass a list of values. Pass a list of values.
# TODO: IntFloatList also inherited all of the methods from the list class. Lets try to test this using .append() to add a new element to our list of type string.
# TODO: Hm, we dont want to be able to append strings to our list. We only want to be able to append int and str. So lets override the append method
# IntFloatList.py
class IntFloatList(list):
def append(self, new_element):
if isinstance(new_element, (int, float)):
super().append(new_element)
else:
print("You can only add ints or floats to this list")