Jed Rembold & Fred Agbo
March 18, 2024
We could accomplish a more more general class attributes by passing arguments to our function:
def create_employee(name, title, salary):
emp = Employee()
emp.name = name
emp.title = title
emp.salary = salary
return empWe could then use that as:
clerk = create_employee('Bob Cratchit', 'clerk', 15)
boss = create_employee(
'Ebeneezer Scrooge', 'founder', 1000
)Employee
class called a constructor, which is responsible for
initializing attributes to a newly created object
__init__selfselfselfs in the class with that object’s name
self is
doingself is always the
first parameter to the __init__ constructor
class Employee:
def __init__(self, name, title, salary):
self.name = name
self.title = title
self.salary = salary
clerk = Employee('Bob Cratchit', 'clerk', 15)
self when creating the object, Python
supplies this reference automaticallyWhat is printed out on the final line of code to the right?
Honda red 2006Honda blue 2006Toyota blue 2008Honda red 2008class Car:
def __init__(self, color, year):
self.color = color
self.year = year
self.make = 'Toyota'
A = Car('blue', 2008)
B = Car('red', 2006)
A.make = 'Honda'
A.year = B.year
print(A.make, A.color, A.year)
Most classes define additional functions called methods to allow clients to read or update attributes or manipulate the object
Methods look like a normal function definition but will
always declare the parameter
self at the beginning of the parameter
list
Methods are defined in the body of the class and would thus look something like:
def method_name (self, other_parameters):
...body of the method...For example
def give_raise(self, amount):
self.salary += amountPython sets self to be a reference to
the receiver, which is the object to which the method is
applied
clerk = Employee('Bob', 'clerk', 15)
clerk.give_raise(15)You retrieve the method from the class itself, and then provide self manually
clerk = Employee('Bob', 'clerk', 15)
Employee.give_raise(clerk, 15)
In the object-oriented model, the client is not supposed to muck-about with the object internals
The implementation should therefore provide methods to retrieve desired attributes (called getters) or to make changes to desired attributes (called setters)
Setting up getters and setters for the attribute
salary might look like:
def get_salary(self):
return self.salary
def set_salary(self, new_salary):
self.salary = new_salaryGetters are far more common than setters, as you don’t always want the client to have the freedom to change attributes on a whim
Printing out an object that you just created as an instance of a custom class will look ugly:
>>> C = Employee('Bob', 'clerk', 15)
>>> print(C)
<__main__.Employee object at 0x7f942ba13550>You can define special methods for a class that specify how your object should be converted to a string (or anything else really)
__str__ or
__repr__ method to specify how your
objectclass Employee:
def __init__(self, name, title, salary):
self.name = name
self.title = title
self.salary = salary
def __str__(self):
return f"{self.name} ({self.title}): {self.salary}"
def get_salary(self):
return self.salary
def set_salary(self, new_salary):
self.salary = new_salary