object oriented programming for python - rijksuniversiteit...

21
Object Oriented Programming for Python Bas Roelenga Rijksuniversiteit Groningen Kapteyn Institute February 20, 2017 Bas Roelenga (RUG) February 20, 2017 1 / 21

Upload: others

Post on 21-Jun-2020

30 views

Category:

Documents


1 download

TRANSCRIPT

Object Oriented Programming for Python

Bas Roelenga

Rijksuniversiteit GroningenKapteyn 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 dataData from this structure can be manipulatedWe 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 YourC la s s :2 de f i n i t ( s e l f ) :

Listing 1: Creating a python class

Two key things to note

classdef __init__(self) (constructor)

Bas Roelenga (RUG) February 20, 2017 4 / 21

The Constructor

What is a constructor?

1 c l a s s YourC la s s :2 de f i n i t ( s e l f , x , y , z ) :3 s e l f . x = x4 s e l f . y = y5 s e l f . z = z

Listing 2: The constructor

The 4 parameters:

self, this is the object itselfx, y and z represent the position of the object

Bas Roelenga (RUG) February 20, 2017 5 / 21

Instantiating our object

How do we create our object?

1 x = 22 y = 33 z = 14

5 ou rC l a s s = YourC la 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 YourC la s s :2 de f i n i t ( s e l f , x ) :3 s e l f . x = x4

5 de 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 YourC la s s :2 de f i n i t ( s e l f , x ) :3 s e l f . x = x4

5 de f getX ( s e l f ) :6 r e t u r n s e l f . x

Listing 5: Our object

1 ou rC l a s s = YourC la s s (2 )2 x = ou rC 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 thevalue of an object

1 c l a s s YourC la s s :2 de f i n i t ( s e l f , x ) :3 s e l f . x = x4

5 de f setX ( s e l f , x ) :6 s e l f . x = x

Listing 7: Implementing a method

1 ou rC l a s s = YourC la s s (2 )2 ou rC l a s s . setX (3 )3

4 p r i n t o u rC 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 theclass

1 c l a s s YourC la s s :2 de f i n i t ( s e l f , x , y ) :3 s e l f . x = x4 s e l f . y = y5

6 de f ge tRad iu 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 willreturn 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 theobject)

These are static elements

1 c l a s s YourC la s s :2

3 c = 3E84

5 de f i n i t ( s e l f , x , y ) :6 s e l f . x = x7 s e l f . y = y

Listing 10: Adding static variable

When creating 2 objects from this class c will remain the same forboth, 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 themin a list:

A 3 dimensional positionA mass

It doesn’t matter what the actual values of the positions or mass endup 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 de f i n i t ( s e l f , x , y , z , mass ) :3

4 s e l f . x = x5 s e l f . y = y6 s e l f . z = z7 s e l f . mass = mass8

9 p a r t i c l e l i s t = [ ]10

11 f o r i i n range (0 , 10) :12

13 p = P a r t i c l e ( i , i , i , i ∗ 10)14 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 de f i n i t ( s e l f , x , y ) :4

5 s e l f . x = x6 s e l f . y = y7

8 de f getX ( s e l f ) :9 r e t u r n s e l f . x

10

11 de f getY ( s e l f ) :12 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 Subc l a s s ( S u p e r c l a s s ) :3

4 de 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 = 13 y = 34

5 ou rC l a s s = Subc l a s s ( x , y )6

7 p r i n t o u rC l a s s . getX ( )8 p r i n t o u rC 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 Subc l a s s ( S u p e r c l a s s ) :3

4 de f i n i t ( s e l f , x , y , mass ) :5 s e l f . x = x6 s e l f . y = y7

8 s e l f . mass = mass9

10 de 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 ) :11 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 ofthe superclass

Bas Roelenga (RUG) February 20, 2017 18 / 21

Exercise 2: Question

Create 3 classes of particles (call them electrons, protons andneutrons)

Every class has the following properties:

A positionAn energy

Every class also has a specific method which can only be used by theclass itself:

Electron: A function that returns the spin (hint: Overload the electronclass constructor)Proton: A function that returns the kinetic energyNeutron: 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 assumeeverything starts with no velocities at all)Create a loop which does a 100 iterations of our N-Body simulationIn each iterations each particles needs to update its position, use thegravitational force between the particles to calculate the acceleration,then use this acceleration to calculate to update the velocity of eachparticle and thus update the position.

Hints:

It doesn’t really matter what values are used for the simulation, weonly want to train our object oriented part.Give every particle an update function, so you only have to call theupdate function and put in the acceleration to update the position ofthe particle.

Bas Roelenga (RUG) February 20, 2017 21 / 21