🐍
[PY] Intro to Computational Media 2.0
CS4All Resources
  • Curriculum Overview
  • How to Use this Curriculum
  • 📬Leave Us Feedback
  • Curriculum Extras
    • 🔮Curriculum Extras Overview
    • Imposter Syndrome (pt. 1)
      • Imposter Syndrome (pt. 2)
    • Timers in Processing.py
    • Time Functions: Crazy Clock Mini Project
    • Pathfinding Algorithms and Facial Scanning
    • Playing with Pixels
  • Unit 1: Draw & Draw With Functions
    • 🔮Unit 1 Overview
    • U1LA1.1: Processing.py Introduction & Deconstruction
    • U1LA1.2 Line Functions and Parameters
    • U1LA1.3: Rectangles, Ellipses, and Layering
    • U1LA1.4: Other Shapes & Styling
    • 🗃️U1LA1 Mini Project: Taijitu Symbol
    • U1LA2.1: Intro to Color with RGB and HSB
    • 🤓EXTRA: Color & Data Storage
    • 🤓EXTRA: Color Palette Practice
    • ✨U1LA3.1: Introducing Variables
    • ✨U1LA3.2: Creating Custom Variables
    • ✨U1LA4.1: What is Abstraction?
    • ✨U1LA4.2: Intro to Functions and Function Calls
    • U1LA4.3: Draw with Functions
    • U1LA4.4: Using the Random Function
    • 🗃️U1LA4 Mini Project: Custom Emoji
    • U1LA5.1: Your Custom Function Library
    • 🎨Unit 1 Final Project: Abstract Album Art
  • Unit 2: Respond and Draw On Canvas
    • 🔮Unit 2 Overview
    • ✨U2LA1.1: Conditionals and If Statements
    • ✨U2LA1.2: Conditionals and if, elif, and else statements
    • ✨U2LA1.3: Logical Operators And/Or
    • 🗃️U2LA1 Mini Project: Make a Traffic Light
    • U2LA2.1: Draw with Mouse
    • U2LA2.2: The Map Function
    • ✨U2LA3.1: Data Type Scavenger Hunt
    • ✨U2LA3.2: Functions that Return Values
    • 🤓U2LA3.3: Functions with Purpose
    • U2LA4.1: Collision Functions and Libraries
    • U2LA4.2: Mouse Clicks and Shapes as Buttons
    • 🗃️U2LA2 Mini Project: Light Switch Game
    • U2LA5.1: Key Presses and Nested Conditionals
    • 🎨Unit 2 Final Project: Interactive Drawing App
  • Unit 3: Loops, Arrays, Media
    • 🔮Unit 3 Overview
    • U3LA1.1: While Loops
    • U3LA1.2: For Loops Pt. 1
    • U3LA1.3: Nested For Loops (For Loops Pt 2)
    • 🗃️U3LA1 Mini Project: Wallpaper Design
    • U3LA2.1: Introduction to Lists
    • U3LA2.2: Random Values from Lists
    • 🗃️U3LA2 Mini Project 1: Fortune Teller
    • U3LA2.3: Loops and Lists
    • U3LA2.4: Updating and Deleting from Lists
    • 🗃️🤓 U3LA2 Mini Project 2: CodeWars List Challenges
    • U3LA3.1: Loading Images
    • 🗃️U3LA3 Mini Project: Vision Board
    • 🎨Unit 3 Final Project: Random Meme Generator
  • Unit 4: Motion, Objects, Transformation
    • 🔮Unit 4 Overview
    • U4LA1.1: Intro to Motion with Flipbooks
    • U4LA1.2: Move in All Directions and Make it Bounce
    • U4LA2.1: Introduction to Classes and Objects
    • U4LA2.2: Reading and Updating Object Properties
    • U4LA2.3: Methods Off Canvas
    • U4LA2.4: Objects on Canvas
    • 🗃️U4LA2 Mini Project: Build a Tamagotchi
    • U4LA3.1: Creating Many Objects
    • 🤓🗃 Optional U4LA3 Mini Project: Rebuild with Objects
    • U4LA4.1: Translation Battleship
    • U4LA4.2: Rotations
    • 🤓U4LA5.1: Sine and Oscillating Motion
    • 🤓U4LA5.2: Cosine and Circular Motion
    • 🎨Unit 4 Final Project: Animated Greeting Card or PSA
  • Unit 5: Final Project
    • 🔮Unit 5 Overview
    • 🎨Course Final Project Guide
Powered by GitBook
On this page
  • Teacher Notes and Overview
  • Objectives
  • Suggested Duration
  • NYS Standards
  • Vocabulary
  • Planning Notes and Materials
  • Resources
  • Assessments
  • Do Now/Warm Up (3 - 5 minutes)
  • Classes Clean-Up (5-10 minutes)
  • Update Properties of an Object (5- 10 minutes)
  • Updating Values: Student Practice (15-25 minutes)
  • Wrap-Up (~5 min)
  • Extension
  1. Unit 4: Motion, Objects, Transformation

U4LA2.2: Reading and Updating Object Properties

How can I read and update properties/attributes of an object?

Teacher Notes and Overview

This lesson builds on what students started in their introduction to objects; it specifically focuses on their ability to read and update object properties in a fairly straightforward, off-canvas practice activity. It's short and meant to reinforce their prior learning.

Objectives

Students will be able to:

  • Read values from an object

  • Update values contained within an object

Suggested Duration

1 Period (~45 minutes)

NYS Standards

9-12.CT.4 Implement a program using a combination of student-defined and third-party functions to organize the computation.

9-12.DL.1 Type proficiently on a keyboard.

Vocabulary

No new vocabulary is introduced

Planning Notes and Materials

Planning Notes
Materials

This is a relatively straight forward lesson; make sure you've reviewed any errors from the previous day as it leaves space to review those.

No specific materials needed

Resources

  • NEED PYTHON READING/UPDATING OBJECTS VIDEO

Assessments

Formative:

  • Student Practice Activity

Summative:

  • Upcoming Mini Project

  • Upcoming Unit Final Project

Do Now/Warm Up (3 - 5 minutes)

Display this Class to your students and ask them to come up with the following:

  1. Which properties/attributes are default values?

  2. What information do you need to provide when making a new object?

  3. How would you create a new object from this class?

  4. What would need to happen if you wanted to create a homeroom property, assuming each student instance will have a different homeroom?

class Student:
    def __init__(self, theName, theGrade, theFavClass):
        self.name = theName
        self.grade = theGrade
        self.school = "CS University"
        self.favClass = "Art"

NB: This example is a little tricky, as theFavClass isn't actually used anywhere in the constructor - so technically, objects could be generated without, and favClass is actually a default value.

Classes Clean-Up (5-10 minutes)

Depending on what you noticed in the previously lesson, you may want to spend some time reviewing any common syntax or conceptual mistakes students are making with classes and objects. A few common ones to look out for:

#Writing the init function without (), which mark it as a function
class Student:
    def __init__:
        self.school = "CS University"
        self.favClass = "Art"
        
#Any other init related typos:
class Student:
    def init():

class Student:
    def _init_():

#Forgetting : at any level:
class Student
    def __init__(self, theName, theGrade, theFavClass)
        self.name = theName
        self.grade = theGrade
        self.school = "CS University"
        self.favClass = "Art"

#Forgetting self, or using a , when it should've been a .
class Student:
    def __init__(theName, theGrade, theFavClass):
        name = theName
        grade = theGrade
        self,school = "CS University"
        self,favClass = "Art"

#Any other variety of typos! It may also be useful to review the difference btween a Class and a single object, and specifically the idea that class names are Capitalized while variables holidng objects are not.

You may want to review all of these, or you may want to focus in on issues that were more specific to what you were seeing from your students.

Update Properties of an Object (5- 10 minutes)

We can think of every property/attribute within an object as it's own individual variable - anything that you can or have done with a variable before, you can do with any single value from an object. That means that just as in the past we have changed values of variables, we can change the values of properties within our objects.

To do this, we use the dot syntax to access the property - we saw this yesterday when printing out out specific values from the objects we had created.

Let's give it a try by creating a new class and an object from that class.

Begin by opening a blank editor and build out the following:

class SubwayStation:
    def __init__(self, name, borough, lines):
        self.name = name
        self.name = borough
        self.lines = lines
        self.elevator = False

Now, let's create an instance of this class. We are going to do "161 St - Yankee Stadium" in our example, but you should select any subway station that is near your school or important to your student population.

class SubwayStation:
    def __init__(self, name, borough, lines):
        self.name = name
        self.name = borough
        self.lines = lines
        self.elevator = False

yankeeStadium = SubwayStation("161 St - Yankee Stadium", "Bronx", ["B", "D", 4])

#let's test that it worked!
print(yankeeStadium)

This should all be review from yesterday. But let's say that we learn that this station actually has an elevator (or one was added, or one got fixed), so the elevator property actually needs to be marked as true. We don't need to start over! We can update that property, like so:

class SubwayStation:
    def __init__(self, name, borough, lines):
        self.name = name
        self.name = borough
        self.lines = lines
        self.elevator = False

yankeeStadium = SubwayStation("161 St - Yankee Stadium", "Bronx", ["B", "D", 4])

#let's test that it worked!
print(yankeeStadium)

#change elevator value
yankeeStadium.elevator = True
print(yankeeStadium.elevator)

This should look very familiar to how we have updated variable values in the past. We can do this for any property, whether it was a default value or a value we gave when creating the new object.

Updating Values: Student Practice (15-25 minutes)

NB: In this example, the Class has been created for them and hidden, along with all of the object instances, in another python file. You can see the file at the top of the screen and see it imported in line 1. This is a great thing to teach students as they continue to create more and more complex projects, sicne their code may become overwhelming if they leave it all in a single document.

Wrap-Up (~5 min)

As in the last lesson, this is a great time to use the wrap-up to review some solutions to the student practice, or review common mistakes. If students worked through it fairly quickly, the majority may want to discuss the spicy challenge option at the end to ensure they know how to complete it.

Extension

Once again, this is a fairly compact lesson without a lot of extension - largely on purpose, as we want to introduce classes and objects slowly to prevent confusion. Ask students what else they would add/change to the class they are working with to make it more useful!

PreviousU4LA2.1: Introduction to Classes and ObjectsNextU4LA2.3: Methods Off Canvas

Last updated 1 year ago

(Trinket)

In this example, we are going to make a class called SubwayStation. Every instance of this class will have a name, borough, subway lines (as an array), and an elevator property, which we will default to false because .

In this practice, students will utilize a with a series of challenges asking them to access information from an object and also update that information. As always, students can work collaboratively or independently to complete these activities.

Starter Code
the MTA has some accessibility issues
starter code