"Build a Simple Ruby on Rails Application" was retired on September 16, 2016.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Basic Object-Oriented Python!
      
    
You have completed Basic Object-Oriented Python!
Preview
    
      
  Start creating object structures using Python's class keyword.
- 
class- the keyword you use in Python to create an object - Instantiation - creating an object from a class by calling the class like a function
 - Instance - the object that was instantiated (created) Ex: 
Car() - 
isinstance(object, type, or class)- check if an object is an instance of the given type or class 
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      You know object-oriented
programming is a way to structure
                      0:00
                    
                    
                      your code into groups of properties and
behaviors.
                      0:04
                    
                    
                      But what does that look
like in Python code?
                      0:08
                    
                    
                      Let's use our car example to build out an
object, or class as it's called in Python.
                      0:13
                    
                    
                      Use the workspaces attached to
this video to follow along.
                      0:20
                    
                    
                      You can also download the file and
use an IDE of your choice if you'd rather.
                      0:24
                    
                    
                      In Python objects are created
using the class keyword and
                      0:33
                    
                    
                      the name of your class
with a colon like this.
                      0:36
                    
                    
                      We're naming our class, Car.
                      0:44
                    
                    
                      Always remembered to try and
name things descriptively when coding.
                      0:46
                    
                    
                      Classes, functions, variables, etcetera.
                      0:50
                    
                    
                      Inside of our class,
we're just going to add, pass.
                      0:54
                    
                    
                      So Python knows to keep moving
instead of giving us an error.
                      0:59
                    
                    
                      If we don't have pass inside of our class,
                      1:04
                    
                    
                      when we run the file,
Python gives us a syntax error.
                      1:06
                    
                    
                      Unexpected end of file while parsing.
                      1:13
                    
                    
                      You may also see a class
written like this,
                      1:20
                    
                    
                      with a set of parentheses
after the name of the class.
                      1:23
                    
                    
                      Classes can take arguments.
                      1:27
                    
                    
                      We won't get into this idea called
inheritance in this course.
                      1:30
                    
                    
                      But I wanted to make sure you know
that a class can be written with or
                      1:35
                    
                    
                      without the parentheses, in case you
come across it out there in the wild.
                      1:39
                    
                    
                      Classes are kind of like a factory.
                      1:44
                    
                    
                      They hold all of the information
about an object like a blueprint.
                      1:47
                    
                    
                      When you use your class to
create an object in Python,
                      1:52
                    
                    
                      it's called instantiation.
                      1:56
                    
                    
                      And the object itself
is called an instance.
                      1:59
                    
                    
                      Let's create one now.
                      2:03
                    
                    
                      You create an instance of class
car like you would a function.
                      2:05
                    
                    
                      Use the name of the class
followed by parentheses.
                      2:08
                    
                    
                      You can also save
the instance to a variable.
                      2:12
                    
                    
                      Now I know our class
doesn't have a lot to it.
                      2:18
                    
                    
                      Well, it has nothing to it right now.
                      2:21
                    
                    
                      But we can still check to
see if we made an object.
                      2:24
                    
                    
                      Let's try printing out our instance.
                      2:27
                    
                    
                      And let's also check its type.
                      2:30
                    
                    
                      print(my_car), print(type(my_car).
                      2:34
                    
                    
                      The first message shows we created
an instance of our car class.
                      2:51
                    
                    
                      It's an object at this location in memory.
                      2:57
                    
                    
                      The second message tells us
our objects type is class car.
                      3:01
                    
                    
                      Cool.
                      3:06
                    
                    
                      There's also a way to check if
something is an instance of a class.
                      3:08
                    
                    
                      You can use a built-in
function called isinstance,
                      3:13
                    
                    
                      and pass in the object you want to check.
                      3:17
                    
                    
                      And the class you want
to check it against.
                      3:20
                    
                    
                      It returns a Boolean value
telling us whether or
                      3:23
                    
                    
                      not the instance is
an instance of the given class.
                      3:26
                    
                    
                      Print(isinstance(my-car and
                      3:32
                    
                    
                      Car with a capital C for the class.
                      3:38
                    
                    
                      Run the file.
                      3:46
                    
                    
                      Our example here is pretty obvious but
it might come in handy later on.
                      3:48
                    
                    
                      I've just thrown a lot of new terms and
information at you.
                      3:54
                    
                    
                      So let me recap.
                      3:58
                    
                    
                      In Python, you create a blueprint for
                      4:00
                    
                    
                      your object using the class keyword,
the name of your class, and a colon.
                      4:03
                    
                    
                      When you create an object using your
class, it's called instantiation.
                      4:09
                    
                    
                      The object you've created
is called an instance.
                      4:14
                    
                    
                      You can use print, type, and isinstance to
see if you've correctly made an object.
                      4:19
                    
                    
                      Try creating a few empty classes of
your own, then create instances of them.
                      4:27
                    
                    
                      Check your work using print,
type, and isinstance.
                      4:33
                    
                    
                      Keep up the hard work Pythonistas.
                      4:37
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up