Object Oriented Programming for Python Bas Roelenga Rijksuniversiteit Groningen Kapteyn Institute February 20, 2017 Bas Roelenga (RUG) February 20, 2017 1 / 21
Why use Object Oriented Programming? Code will be more readable and organized There is less duplicate code, i.e. a more efficient program Makes it easier to write larger programs Bas Roelenga (RUG) February 20, 2017 2 / 21
What is Object Oriented Programming? A type of programming which uses object These objects can be everything What is an object? A structure that contains data Data from this structure can be manipulated We can request the data from this structure Bas Roelenga (RUG) February 20, 2017 3 / 21
Syntax structure of Object Oriented Programming Constructing a class 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f ) : Listing 1: Creating a python class Two key things to note class def init (self) (constructor) Bas Roelenga (RUG) February 20, 2017 4 / 21
The Constructor What is a constructor? 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x, y, z ) : 3 s e l f. x = x 4 s e l f. y = y 5 s e l f. z = z Listing 2: The constructor The 4 parameters: self, this is the object itself x, y and z represent the position of the object Bas Roelenga (RUG) February 20, 2017 5 / 21
Instantiating our object 1 x = 2 2 y = 3 3 z = 1 4 How do we create our object? 5 o u r C l a s s = Y o u r C l a s s ( x, y, z ) Listing 3: Creating our object Bas Roelenga (RUG) February 20, 2017 6 / 21
Adding a method(function) to our object Why add a method to our object? 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f getx ( s e l f ) : 6 r e t u r n s e l f. x Listing 4: Implementing a method When calling the function getx() it will return the x value Also called a getter function Bas Roelenga (RUG) February 20, 2017 7 / 21
Using our object 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f getx ( s e l f ) : 6 r e t u r n s e l f. x Listing 5: Our object 1 o u r C l a s s = Y o u r C l a s s ( 2 ) 2 x = o u r C l a s s. getx ( ) 3 4 p r i n t x Listing 6: Obtaining data from our object Bas Roelenga (RUG) February 20, 2017 8 / 21
Adding more methods(functions) to our object Instead of getting a value from our object we can also change the value of an object 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x ) : 3 s e l f. x = x 4 5 d e f setx ( s e l f, x ) : 6 s e l f. x = x Listing 7: Implementing a method 1 o u r C l a s s = Y o u r C l a s s ( 2 ) 2 o u r C l a s s. setx ( 3 ) 3 4 p r i n t o u r C l a s s. getx ( ) Listing 8: Obtaining data from our object Bas Roelenga (RUG) February 20, 2017 9 / 21
Adding more methods(functions) to our object Add a function which returns an operation on data members of the class 1 c l a s s Y o u r C l a s s : 2 d e f i n i t ( s e l f, x, y ) : 3 s e l f. x = x 4 s e l f. y = y 5 6 d e f g e t R a d i u s ( s e l f ) : 7 r e t u r n np. s q r t ( s e l f. x 2 + s e l f. y 2) Listing 9: Implementing a method When calling the function getradius() of the object, the object will return the radius Bas Roelenga (RUG) February 20, 2017 10 / 21
Adding a class variable Class variables are variables that are a part of the class (not of the object) These are static elements 1 c l a s s Y o u r C l a s s : 2 3 c = 3E8 4 5 d e f i n i t ( s e l f, x, y ) : 6 s e l f. x = x 7 s e l f. y = y Listing 10: Adding static variable When creating 2 objects from this class c will remain the same for both, thus if we change c it will change for both objects Bas Roelenga (RUG) February 20, 2017 11 / 21
Exercise 1: Question Code 10 particles which have the following parameters and store them in a list: A 3 dimensional position A mass It doesn t matter what the actual values of the positions or mass end up being. Bas Roelenga (RUG) February 20, 2017 12 / 21
Exercise 1: Answer 1 c l a s s P a r t i c l e : 2 d e f i n i t ( s e l f, x, y, z, mass ) : 3 4 s e l f. x = x 5 s e l f. y = y 6 s e l f. z = z 7 s e l f. mass = mass 8 9 p a r t i c l e l i s t = [ ] 0 1 f o r i i n range ( 0, 10) : 2 3 p = P a r t i c l e ( i, i, i, i 10) 4 p a r t i c l e l i s t. append ( p ) Listing 11: Answer Bas Roelenga (RUG) February 20, 2017 13 / 21
Inheritance What is inheritance? When is inheritance useful, i.e why do we want to use it? There are 2 kinds of classes now: Superclass (can also be called an abstract class) Subclass Bas Roelenga (RUG) February 20, 2017 14 / 21
Using inheritance: superclass The superclass looks like any other class This class will serve as a abstract for our subclasses 1 c l a s s S u p e r c l a s s : 2 3 d e f i n i t ( s e l f, x, y ) : 4 5 s e l f. x = x 6 s e l f. y = y 7 8 d e f getx ( s e l f ) : 9 r e t u r n s e l f. x 0 1 d e f gety ( s e l f ) : 2 r e t u r n s e l f. y Listing 12: Our superclass Bas Roelenga (RUG) February 20, 2017 15 / 21
Using inheritance: subclass The subclass looks a little bit different 1 2 c l a s s S u b c l a s s ( S u p e r c l a s s ) : 3 4 d e f g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( s e l f ) : 5 r e t u r n s e l f. x s e l f. y Listing 13: Our subclass Notice that this subclass does not contain a constructor Bas Roelenga (RUG) February 20, 2017 16 / 21
Using inheritance: putting it all together We create the a class in the following way: 1 2 x = 1 3 y = 3 4 5 o u r C l a s s = S u b c l a s s ( x, y ) 6 7 p r i n t o u r C l a s s. getx ( ) 8 p r i n t o u r C l a s s. g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( ) Listing 14: Putting it together We can use all the function and variables from the superclass Only our subclass can use its specific property method!! Bas Roelenga (RUG) February 20, 2017 17 / 21
Overloading What is overloading? 1 2 c l a s s S u b c l a s s ( S u p e r c l a s s ) : 3 4 d e f i n i t ( s e l f, x, y, mass ) : 5 s e l f. x = x 6 s e l f. y = y 7 8 s e l f. mass = mass 9 0 d e f g e t S u b c l a s s S p e c i f i c P r o p e r t i e ( s e l f ) : 1 r e t u r n s e l f. x s e l f. y Listing 15: Overloading The constructor function now overloads the constructor function of the superclass Bas Roelenga (RUG) February 20, 2017 18 / 21
Exercise 2: Question Create 3 classes of particles (call them electrons, protons and neutrons) Every class has the following properties: A position An energy Every class also has a specific method which can only be used by the class itself: Electron: A function that returns the spin (hint: Overload the electron class constructor) Proton: A function that returns the kinetic energy Neutron: A function that returns the mass Bas Roelenga (RUG) February 20, 2017 19 / 21
Exercise 2: Answer First of we create the superclass Particle We create a subclass for each particle For electrons we overload the constructor For protons and neutrons we give there mass as a static variable We create the function that does each of the operations Bas Roelenga (RUG) February 20, 2017 20 / 21
If there is time left Create a simple N-body simulation using object oriented programming The following steps can be followed: Create a 100 particles with x, y, z and mass parameters (just assume everything starts with no velocities at all) Create a loop which does a 100 iterations of our N-Body simulation In each iterations each particles needs to update its position, use the gravitational force between the particles to calculate the acceleration, then use this acceleration to calculate to update the velocity of each particle and thus update the position. Hints: It doesn t really matter what values are used for the simulation, we only want to train our object oriented part. Give every particle an update function, so you only have to call the update function and put in the acceleration to update the position of the particle. Bas Roelenga (RUG) February 20, 2017 21 / 21