Part 3 of 43
This is the third in a series of questions and answers about core data science topics. See more standards on my Data Science Standards page.
3. OOP
- Given the code for a python class, instantiate a python object and call the methods and list the attributes.
- Write the python code for a simple class.
class ClassName:
def __init__(self, parameter1, p2):
self.var = parameter1
self.var2 = p2
def __repr__(self):
"""Return a text description."""
return "{} of {}".format(self.var, self.var2)
@property # Field can't be changed w/o making setter (@color.setter)
def color(self):
return self._c_map[self.p2]
def __call__(self, elem):
'''Perform map operation on an element'''
return self._impl(elem)
def _impl(self, elem):
pass
def method_name(self, p3):
self.var2 += self.var * p3
-
Match key “magic” methods to their functionality. Full list/tutorial here: http://www.diveintopython3.net/special-method-names.html
-
Design a program or algorithm in object oriented fashion.
- Compare and contrast functional and object oriented programming.
- In OOP the user creates and defines a new data type, which it's own attributes (data), and methods (operations). This new class can then be used to create objects.
- In functional programming, your program just uses functions and does not create an entirely new class to organize data around.
- Functional programming typically is faster, takes up less memory, but can less organized and difficult for humans to interpret if there's a lot going on.