Object Oriented Programming

Raizel Bernstein
DataDrivenInvestor
Published in
3 min readDec 31, 2020

--

Gingerbread Class

It was December 25th, two thousand and twenty-two. The world was is in disarray. To remedy bad vibes radiating from 2020, I knew I needed to distract myself with something fun. I felt it was my job to bring joy back into my life, and the lives of my mother, grandmother, and brother. I thought, what COVID-19 safe activity could we do, for Christmas? Like many others, we settled on building gingerbread houses. The caveat being, we weren’t able to purchase kits because they sold out. Instead, we purchased graham crackers, icing, skittles, jelly beans, saltine crackers, and some other candy. It’s way more fun to build from scratch. Plus, the kits are expensive. Also, my one job of ‘bringing joy’ doesn’t provide an income.

I decided to make building gingerbread houses a competition, and my family agreed. The judges of the competition were my mom’s Facebook friends. Based on the number of likes my gingerbread house received, I assume she didn’t have many Facebook friends. Fortunately, I won!

brother, grandmother, me, mother

Building the Gingerbread Houses in Python

A class is typically thought of as blueprint. In this case, we’ll create a blueprint for gingerbread houses. Our first step is to define the class.

class Gingerbread_House:

Next we will use a constructor method to build a class. __init__: the constructor and the __init__ constructor is a dunder method: indicated by the double underscore in front and after init

def __init__

After declaring the constructor we add self. Self is an instance of the class Gingerbread_House. Gingerbread_House is the object created. Self, name, sober, and contents are the parameters.

def __init__(self, name, sober, contents): 

Name, sober, and contents are local variables within the init method. Self.name, self.sober, self.contents are the attributes of the Gingerbread_House class. Attributes can be called anywhere inside the class.

self.name = name
self.sober = sober
self.contents = contents

Name will take a string argument. Sober will take in a boolean argument. Contents will take in a list argument.

So far all together we have:

Functions Within a Class

Any function within a class is a method. Self will always be one of the parameters.

def is_sober(self):
if self.sober:
return 'drink more'
else:
return 'drunk'

Self is the instance of Gingerbread_House and is being passed in to the method is_sober. Self.sober is an attribute. As stated earlier, attributes can exist anywhere inside the class.

def build(self):
for i in self.contents:
if i == 'graham crackers':
print('necessary foundation')
elif i == 'icing':
print('the glue that holds it all together')
elif i == 'jelly beans' or 'gummies' or 'skittles':
print('candy decorations')
def family(self):
if self.name == 'Ana':
return 'grandma'
elif self.name == 'Bebe':
return 'Momager'
elif self.name = 'Marc':
return 'brother'
elif self.name == 'Raizel':
return 'the sun ... everything revolves around her'
else:
return 'who are you'

Two more methods were created. def build uses the attribute self.contents to show the building blocks of the gingerbread house. def family uses the attribute self.name to display the name of the competitor.

Finally

Applying Gingerbread House Class in Code

person_(number) is the variable assigned to specific instances. When different methods and/or attributes are called, the person_(number) becomes equivalent to self. Each person is passed a set of arguments denoted by the constructor method.

print(person_2.sober)
print(person_4.contents)
print(person_5.contents)

As seen above, when each attribute is printed, the makeup of the attribute are outputted to the consol.

person_1.build()
print()
print(person_2.is_sober())
print(person_3.family())

When the methods are called, it runs, and the result is printed out.

Gain Access to Expert View — Subscribe to DDI Intel

--

--