Git Product home page Git Product logo

dsc-1-07-04-object-initialization-lab-online-ds-ft-021119's Introduction

Object Initialization - Lab

Introduction

In this lab, we will practice defining classes with custom __init__ methods. We will define two classes, Driver and Passenger in the cells below. Follow the instructions in order to get the tests to pass.

Objectives

You will be able to:

  • Define custom __init__ methods for object initialization
  • Create instance variables in the __init__ method
  • Use default arguments in the __init__ method

Initializing Instance Objects

Let's start off by defining Driver and Passenger classes. In the Passenger class, we will want our passengers to have the attributes first, last, email, and rides_taken for their first name, last name, email, and number of rides they have taken. Let's provide our __init__ method with the default argument of 0 for the rides_taken attribute since new passengers should not have taken any rides.

After we've defined our Passenger __init__ method, check it out by initializing a new passenger with the first name "Rebecca", the last name "Black", and the email "[email protected]". Don't worry about creating instance methods or using any attribute decorators in this lesson. We can just instantiate instance variables and use those to access the instance's attributes.

# Define Passenger Class Here
rebecca_black = None # initialize Rebecca Black here
print(rebecca_black.first) # "Rebecca"
print(rebecca_black.last) # "Black"
print(rebecca_black.email) # "[email protected]"
print(rebecca_black.rides_taken) # 0

rebecca black in a car

Great work! Rebecca Black is now in the system and ready to request her ride to the party on Friday! Friday! Friday!

In the Driver class, define an __init__ method that initializes a driver with the attributes first, last, and favorite_hobby for their first name, last name, and favorite hobby. Let's provide a default argument of "driving" for favorite_hobby, since we would hope they are doing what they love!

After we have our Driver __init__ method, let's initialize a driver with the first name "Dale", last name "Earnhardt". Again, don't worry about creating any instance methods or using any property decorators.

# Define Passenger Class Here
dale_earnhardt = None # initialize Dale Earnhardt here
print(dale_earnhardt.first) # "Dale"
print(dale_earnhardt.last) # "Earnhardt"
print(dale_earnhardt.favorite_hobby) # "driving"

DaleEarnhardt

Awesome, maybe Dale will be Rebecca's driver to the party on Friday! Friday! Friday!

Summary

In this lab, we practiced definining custom __init__ methods that allowed us to initialize new instances with a set of predetermined attributes and default attributes.

dsc-1-07-04-object-initialization-lab-online-ds-ft-021119's People

Contributors

loredirick avatar mathymitchell avatar peterbell avatar sik-flow avatar

Watchers

Kevin Ernest Long avatar James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar  avatar  avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.