Search Header Logo
Intro to Object Oriented Programming: Class Attributes

Intro to Object Oriented Programming: Class Attributes

Assessment

Presentation

Computers

6th - 8th Grade

Practice Problem

Medium

Created by

Kimberly Lewis

Used 6+ times

FREE Resource

15 Slides • 5 Questions

1

media

Object Oriented Programming: Class Attributes

2

​What are some things each of them has?​

I have a group of people.

media

3

media
  • Name

  • Age

  • Height

  • Weight

  • Hair color

  • Eye color

  • Skin color

4

media

If we think about data types, we could consider doing all of this in lists based on individuals. Then we can pull the information from the list.

Lists

5

  • Every list is new information!

  • How big can we really make the lists?

  • What if you don't have the same number of fields?

  • No inheritance (we'll cover this later!)

Cons

  • You've done it before

  • Relatively simple

Pros

Pros and Cons to Lists

6

media

Classes let us make smaller files, and more advanced programs

7

media

Let's look at the Enterprise crew as a class.

8

media

An instance is a single crew member.

9

Think of it this way...

A class is a blueprint.

The instance is the actual data.

10

Multiple Choice

You want to create a game that has several people playing at once. Should you build it using a list for each player or a class?

1

Lists

2

Class

11

Multiple Choice

Your game has a bunch of different food items the characters eat to gain health. Would you create a class just for the food?

1

Yes

2

No

12

Multiple Choice

Now you plan on having players fight monsters in the game. Should the monsters have their own class?

1

Yes

2

No

13

media

We start by adding the class statement. This tells python that everything after it is a class. Then we name it. pass is just a way to tell the code that there will be code here.

So how do we code it?

14

media

We create a function called __init_ that we add some of the properties we want in all of the dogs.

Now we give it properties.

15

What's in the parentheses?

These are called arguments. This is telling the code exactly how many and where we will be specifying this information when we make our instance. It reminds the code what it needs when you make an instance, and where to put that information.

16

media

The code self.name = name creates an attribute called "name" and assigns the value of the variable "name" to it. The same goes for the age and the breed.

Back to the code...

17

Fill in the Blank

Question image

Which piece of code creates the attribute "name" and assigns it the value "name"? Write the actual code, not the line number!

.
=

18

media

Just like when we call a function, we put the name of the class with the parentheses to tell the computer we're creating a new instance. But look at the red squigglies! We're not done yet!

Running the class

19

media

Now we add our instance information to the class in the parentheses! We have an instance of Dog()!

We add the variable information here!

20

Multiple Choice

Question image

What's wrong with the code here?

1

Nothing

2

You're calling a function with a variable

3

You haven't added the instance values to the class

4

The name in the variable isn't capitalized

media

Object Oriented Programming: Class Attributes

Show answer

Auto Play

Slide 1 / 20

SLIDE