40
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Classes and Objects

Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Introduction

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Classes and Objects

Page 2: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Page 3: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractions

Page 4: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

Page 5: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

make one thing act like another

Page 6: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

make one thing act like another

Functions turn many steps into one (logical) step

Page 7: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

make one thing act like another

Functions turn many steps into one (logical) step

Libraries group functions to make them manageable

Page 8: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

make one thing act like another

Functions turn many steps into one (logical) step

Libraries group functions to make them manageable

Classes and objects combine functions and data

Page 9: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Computer science is the study of algorithms

Computer programming is about creating and

composing abstractionshide the details

make one thing act like another

Functions turn many steps into one (logical) step

Libraries group functions to make them manageable

Classes and objects combine functions and data

And, if used properly, do much more as well

Page 10: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

Page 11: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants

Page 12: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails

Page 13: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

Page 14: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move

photosynthesize

Page 15: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D

photosynthesize scavenge

Page 16: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D

photosynthesize scavenge hunt

Page 17: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D

photosynthesize scavenge hunt

Algorithm is simple

Page 18: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D

photosynthesize scavenge hunt

Algorithm is simple for t inrange(timesteps):move(world, everything)eat(world, everything)show(world, everything)

Page 19: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D

photosynthesize scavenge hunt

Algorithm is simple

Program is more complicated

for t inrange(timesteps):move(world, everything)eat(world, everything)show(world, everything)

Page 20: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def move(world, everything):for thing in everything:if thing[0] == 'plant':pass # plants don't move

elif thing[0] == 'snail':move_snail(snail)

elif thing[0] == 'fish':move_fish(fish)

Page 21: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def move(world, everything):for thing in everything:if thing[0] == 'plant':pass # plants don't move

elif thing[0] == 'snail':move_snail(snail)

elif thing[0] == 'fish':move_fish(fish)

So far, so good

Page 22: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def eat(world, everything):for thing in everything:if thing[0] == 'plant':photosynthesize(world, plant)

elif thing[0] == 'snail':scavenge(world, snail)

elif thing[0] == 'fish':prey = hunt(world, everything,

thing)if prey != None:devour(world, everything,

thing, prey)

Page 23: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def eat(world, everything):for thing in everything:if thing[0] == 'plant':photosynthesize(world, plant)

elif thing[0] == 'snail':scavenge(world, snail)

elif thing[0] == 'fish':prey = hunt(world, everything,

thing)if prey != None:devour(world, everything,

thing, prey)Hmm…

Page 24: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def show(world, everything):show_world(world)for thing in everything:if thing[0] == 'plant':show_plant(plant)

elif thing[0] == 'snail':show_snail(snail)

elif thing[0] == 'fish':show_fish_fish)

Page 25: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

def show(world, everything):show_world(world)for thing in everything:if thing[0] == 'plant':show_plant(plant)

elif thing[0] == 'snail':show_snail(snail)

elif thing[0] == 'fish':show_fish_fish)

This is starting to look familiar…

Page 26: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Pessimist: code that's repeated in two or more

places will eventually be wrong in at least one

Page 27: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Pessimist: code that's repeated in two or more

places will eventually be wrong in at least one

To add starfish, we have to modify three functions

Page 28: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Pessimist: code that's repeated in two or more

places will eventually be wrong in at least one

To add starfish, we have to modify three functions

remember to

Page 29: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Pessimist: code that's repeated in two or more

places will eventually be wrong in at least one

To add starfish, we have to modify three functions

remember to

What about fish that eat plants? Or scavenge?

Page 30: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Pessimist: code that's repeated in two or more

places will eventually be wrong in at least one

To add starfish, we have to modify three functions

remember to

What about fish that eat plants? Or scavenge?

Optimist: every pattern in a program is an

opportunity to shorten that program

Page 31: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Wouldn't this be simpler?

Page 32: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

for thing in everything:thing.move()prey = thing.eat(everything)if prey:thing.devour(prey)everything.remove(prey)

Wouldn't this be simpler?

Page 33: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Wouldn't this be simpler?

Easier to understand (after some practice)

for thing in everything:thing.move()prey = thing.eat(everything)if prey:thing.devour(prey)everything.remove(prey)

Page 34: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Wouldn't this be simpler?

Easier to understand (after some practice)

Much easier to add new kinds of things

for thing in everything:thing.move()prey = thing.eat(everything)if prey:thing.devour(prey)everything.remove(prey)

Page 35: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Nothing is free

Page 36: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex

Page 37: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex

And too much abstraction creates as big a mental

burden as too little

Page 38: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex

And too much abstraction creates as big a mental

burden as too little

Degree of Abstraction

Men

tal E

ffort

Req

uire

d

Putting steps togetherto get big picture

Page 39: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex

And too much abstraction creates as big a mental

burden as too little

Degree of Abstraction

Men

tal E

ffort

Req

uire

d

Putting steps togetherto get big picture

Tracing steps to figure outwhat actually happens

Page 40: Classes and Objects - v4.software-carpentry.org · Classes and objects combine functions and data. Classes and Objects Introduction Computer science is the study of algorithms Computer

January 2011

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

created by

Greg Wilson