Intro to Python: Object Oriented Programming¶
Example 1: Importing & Instantiating a class¶
from timewithproperties import Time #comment: see source [1]
wake_up = Time(hour=7,minute=45,second=30)
print(wake_up.time)
timewithproperties.py
Example 2: Using a function within a class¶
from timewithproperties import Time
wake_up2 = Time(hour=7,minute=45,second=30)
wake_up2.add_time(hour=4,minute=5,second=10)
print(wake_up2.time)
Example 3: Using Class doc strings¶
Show the doc strings associated with;
* Time class
* the add_time
function in the time class
Example 4: Building a class doc string¶
class demo:
"""This is the class doc string."""
def __init__(self,variable1):
self.variable1 =variable1
def print_variables(self):
"""This is the doc string for this function in this class"""
print(self.variable1)
d = demo('hello')
d.print_variables()
Example 5: Public vs Private Attributes¶
- The
__
underscore at the beginning of the class is what defines the private vs public property; e.g.self.__private_data
class Private:
def __init__(self):
self.public_data="public"
self.__private_data="private"
p = Private()
print(p.public_data)
print(p.__private_data)
Example 6: Decorators¶
In this example we create a decorator function that takes an arbitrary python function and multiplies it's output by a factor of 10.
def multiply_by10(func):
def inner(*args,**kwargs):
return 10*func(*args,**kwargs)
return inner
@multiply_by10
def add_two_numbers(a,b):
return a+b
add_two_numbers(3,5)
Example 7: Read only vs changeable properties¶
- Class with a property setter
class property_setter:
def __init__(self,alpha):
self._a = alpha
@property
def a(self):
return self._a
@a.setter
def a(self,alpha):
self._a = alpha
p7 = property_setter('hello')
print(p7._a)
p7._a='good bye'
print(p7._a)
Class **without a property setter, for mutability**
class property_no_setter:
def __init__(self,alpha):
self._a = alpha
@property
def a(self):
return self._a
p71 = property_no_setter('hello')
print(p71.a)
p71.a='good bye'
print(p71.a)
Example 8: Representing a class¶
class demo_repr_method:
def __init__(self,alpha):
self.alpha = alpha
def __repr__(self):
"""Return initialization strin for the class"""
return (f'demo_repr_method(alpha={self.alpha})')
What happens if we change the parameters?
Sources¶
- [1] Timewithproperties.py - this file taken from the textbook "Intro to Python for Computer Science and Data Science" By Deitel & Deitel, Pearson Publications 2020