Git Product home page Git Product logo

dsc-object-initialization-lab-dc-ds-010620's Introduction

Object Initialization - Lab

Introduction

In this lab, you'll practice defining classes with __init__ methods. You'll define two classes, Driver and Passenger in the cells below.

Objectives

In this lab you will:

  • Create instance variables in the __init__ method
  • Use default arguments in the __init__ method

Initializing Instance Objects

Start off by defining the Driver class, similar to as you've done before. This time, define an __init__ method that initializes a driver with the attributes first, last, and occupation for their first name, last name, and occupation. Provide a default argument of "driver" for occupation.

# Define Driver class here

Now, initialize a driver with the first name "Dale" and last name "Earnhardt".

gif from Nascar

dale_earnhardt = None # Initialize Dale Earnhardt here
print(dale_earnhardt.first) # "Dale"
print(dale_earnhardt.last) # "Earnhardt"
print(dale_earnhardt.occupation) # "driving"

Next, define the Passenger class. Using the __init__ method, ensure all instances contain the attributes first, last, email, and rides_taken for their first name, last name, email, and number of rides they have taken. Provide the __init__ method with the default argument of 0 for the rides_taken attribute since new passengers should not have taken any rides.

# Define Passenger class here

Now that you've defined a Passenger class, check it out by initializing a new passenger with the first name "Jerry", the last name "Seinfeld", and the email "[email protected]".

jerry = None # Initialize Mr. Seinfeld here
print(jerry.first) # "Jerry"
print(jerry.last) # "Seinfeld"
print(jerry.email) # "[email protected]"
print(jerry.rides_taken) # 0

Great work! Mr. Seinfeld is now in the system and ready to request a ride!

Summary

In this lab, you defined __init__ methods that allowed you to initialize new instances with a set of predetermined attributes and default attributes.

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.