Intro to
Computer
Science

Spring 2021
course
site
-->

April 15

... start recording.

Questions about anything?

You should all be in the throes of the STOP game ... how is that going?

I gave some ideas about how to organize the game at the top level ... you don't need to do things this way, but just to help your thinking along. If you do use any of those ideas, do quote your source (me; the file) when you turn things in. I've attached it to the bottom of this page.

OK - onward and upward.

I created three python files during our class today, STOP_thinking.py, card.py, and vector.py ; all are attached below. (There is also another longer card.py in my homework answer.)

card.py

First, I'd like to work on the card.py Blackjack playing card ... I think from looking at what you've turned in, many of you have been spending most of your time on the project, not the object stuff. (Which I completely understand.)

We'll see which way you'd like to work on that :

inheritance

Next I'd like to discuss another object concept : "inheritance".

Here's an barnyard.py example which I will walk us through.

Vocabulary :

The GraphicsObject parent class and Point, Line, Circle, Rectangle, ... child classes in Zelle's graphics library are a good example of this idea.

special methods

Another object topic that is worth at least mentioning are the many "special methods" that let object classes behave like lists, functions, numbers, or other python types.

I discuss that some in my notes on objects, and we should at least glance at the documentation .

In particular , in addition to __init__ here are some of the "special methods" that can let your class behave like pretty much any other type of thing we've talked about so far: numbers, strings, functions, lists, ...

__str__          print(thing)       Convert to string!
__len__          len(thing)         It's a container!
__getitem__      a = thing[i]       It's a list!
__setitem__      thing[i] = a       That you can modify!
__contains__     x in thing         And test if something's in it!
__call__         thing(a)           No wait - it's a function!
__add__          thing + a          No, it's a number!

It's a bird! It's a plane! It's ... SUPER OBJECT!

(No wait - that's inheritance.)

In general, this sort of idea is usually called overriding .

We may try an example or two in class.

data structures

Depending on how far we get, we may start in on the material in chapter 11, on data_structures, starting with the "dictionary" type .

And once we've seen the dictionary : re-write card.py to use them ...

https://cs.bennington.college /courses /spring2021 /introcs /notes /april15
last modified Thu April 15 2021 11:50 am

attachments [paper clip]

  last modified size
TXT STOP_thinking.py Thu Apr 15 2021 11:49 am 811B
TXT card.py Thu Apr 15 2021 11:49 am 1.6K
TXT vector.py Thu Apr 15 2021 11:49 am 734B