Fred Agbo
October 28, 2026
ZvG9U
GRect class.
GRect classClass definitions in Python start with a header line consisting
of the keyword class and then the class
name
The body of the class will later contain definitions, but initially can just leave blank
pass keywordclass Employee:
"""This class is currently empty!"""Once the class is defined, you can create an object of this class type by calling the class as if it were a function:
clerk = Employee()You can select an attribute from an object by writing out the object name, followed by a dot and then the attribute name.
As an example
clerk.name
would select the name attribute for the
clerk object
Attributes are assignable, so
clerk.salary *= 2
would double the clerk’s current salary
You can create a new attribute in Python by simply assigning a name and a value, just like you’d define a new variable
We could, for instance, create a
clerk in the following fashion:
def create_clerk():
clerk = Employee()
clerk.name = "Bob Cratchit"
clerk.title = "clerk"
clerk.salary = 15
return clerkNote that none of these assigned attributes affect the
Employee class in any way
We could accomplish this more generally 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 automaticallyMost 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)
–
What would be the output of the printed statement in the code to the right?
(1, 'a', 'b')(1, 'a')A = (1, 3, 5)
B = (2*A, ('a', ) )
C = B + ('b', 'c', 'd')
D = tuple()
for v in C[:3]:
D += v[:1]
print(D)
–
What 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)
A.make = 'Honda'
B = Car('red', 2006)
A.year = B.year
print(A.make, A.color, A.year)
title, artist,
and duration_secgenres in your class, and so add as an
attributegenres attribute? You get to choose, but
have a reason for why you chose what you did!playlist