Classes: Object Oriented Programming
OBJECTS
1234 3.14159 "Hello" [1, 5, 7, 11, 13]
{"CA": "California", "MA": "Massachusetts"}
OBJECT ORIENTED PROGRAMMING (OOP)
WHAT ARE OBJECTS?
that captures…
(aka procedures/functions)
EXAMPLE:
[1,2,3,4] has type list
L =
1 -> 2 -> 3 -> 4 ->
CREATING AND USING YOUR OWN TYPES WITH CLASSES
using an instance of the class
Implementing the class Using the class
HIERARCHIES
Animal
Cat
Rabbit
Person
(superclass)
(subclass)
Student
Note: CompuCell3D Python modeling is done using basic hierarchies!
DEFINE YOUR OWN TYPES
class Coordinate(object):
#define attributes here
Implementing the class Using the class
WHAT ARE ATTRIBUTES?
DEFINING HOW TO CREATE AN INSTANCE OF A CLASS
initialize some data attributes
class Coordinate(object):
def init (self, x, y):
self.x | = | x |
self.y | = | y |
Implementing the class Using the class
ACTUALLY CREATING AN INSTANCE OF A CLASS
c = Coordinate(3,4) origin = Coordinate(0,0) print(c.x) print(origin.x)
Implementing the class Using the class
WHAT IS A METHOD?
DEFINE A METHOD FOR THE
Coordinate CLASS
class Coordinate(object): def init (self, x, y):
self.x = x self.y = y
def distance(self, other): x_diff_sq = (self.x-other.x)**2 y_diff_sq = (self.y-other.y)**2
return (x_diff_sq + y_diff_sq)**0.5
Implementing the class Using the class
HOW TO USE A METHOD
def distance(self, other):
# code here
Using the class:
c = Coordinate(3,4) zero = Coordinate(0,0)
print(c.distance(zero))
c = Coordinate(3,4) zero = Coordinate(0,0)
print(Coordinate.distance(c, zero))
Implementing the class
Using the class
PRINT REPRESENTATION OF AN OBJECT
>>> c = Coordinate(3,4)
>>> print(c)
< main .Coordinate object at 0x7fa918510488>
print on your class object
Coordinate object, want to show
>>> print(c)
<3,4>
DEFINING YOUR OWN PRINT METHOD
class Coordinate(object):
def init (self, x, y):
self.x = x
self.y = y
def distance(self, other):
x_diff_sq = (self.x-other.x)**2 y_diff_sq = (self.y-other.y)**2
return (x_diff_sq + y_diff_sq)**0.5
def __str__(self):
return "<"+str(self.x)+","+str(self.y)+">"
Implementing the class Using the class
WRAPPING YOUR HEAD AROUND TYPES AND CLASSES
>>> c = Coordinate(3,4)
>>> print(c)
<3,4>
>>> print(type(c))
<class main .Coordinate>
>>> print(Coordinate)
<class main .Coordinate>
>>> print(type(Coordinate))
<type 'type'>
>>> print(isinstance(c, Coordinate)) True
Implementing the class Using the class
SPECIAL OPERATORS
+, -, ==, <, >, len(), print, and many others
https://docs.python.org/3/reference/datamodel.html#basic-customization
add _(self, other) | → | self | + other |
sub (self, other) | → | self | - other |
eq _(self, other) | → | self | == other |
lt _(self, other) | → | self | < other |
len (self) | → | len(self) | |
str (self) ... and others | → | print self | |
EXERCISE: COORDINATES
Coordinates objects
THE POWER OF OBJECT ORIENTED PROGRAMMING
Select Features for Computational Modeling and Scientific Computing
RANDOM NUMBERS
Return random number in [0, 1) (uniform)
Return random integer in [a, b]
Return random element in sequence seq
Return random number in [a, b] (uniform)
Return random number with mean mu and
standard deviation sigma (Gaussian)
Think reproducibility!
TIME
Returns the epoch of your machine
Returns the number of seconds since the epoch
Pause the program for x seconds
MATH
Useful constants!
Rounding functions
Error function, exponential, log…
As the name suggests…
Square root
Trig functions
STATISTICS
Mean of a sequence
Median of a sequence
Standard deviation of a sequence
Returns boundaries of n quantiles of a sequence
EXERCISE: RANDOM WALK
Module Questionnaire
Please take a minute or two to let us know about your experience with this module by filling out the brief zoom survey
Feel free to provide additional comments and suggestions in the slack or by email to us as well (hfennel@iu.edu)
Funding Sources: NIH U24 EB028887, NSF 2120200, 2000281, 1720625
Previous Funding : NIH R01 GM122424