Lesson 45 of 60
โฎPrev Nextโฏ

PY-101 ยท Object-oriented Python

Classes and objects

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

45of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A class is a blueprint that describes a kind of thing, and an object is one actual thing made from that blueprint. You define a class with the class keyword and create objects by calling it like a function, such as Dog(). Each object keeps its own data, so two dogs can have different names.

    • Define a class
    • Create objects from it
    • Give each object its own data

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Classes and objects, running
    1. class Dog:
    2. pass
    3. rex = Dog()
    4. rex.name = "Rex"
    5. bella = Dog()
    6. bella.name = "Bella"
    7. print(rex.name, bella.name)
    8. print(type(rex).__name__, isinstance(bella, Dog))
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA school app tracks 900 students. Each has a name, a class and marks. A Student blueprint, used 900 times, keeps every student's data together and tidy.
    2. The Python ideaclass Name:A class is a blueprint that describes a kind of thing, and an object is one actual thing made from that blueprint.
    3. In real lifeIn Django every table is a class, and every row you load from the database becomes an object of that class.

    Start with a question

    A school app tracks 900 students. Each has a name, a class and marks. A Student blueprint, used 900 times, keeps every student's data together and tidy.

    Most large Python programs, frameworks and libraries are organised into classes. Reading them needs this foundation.

    Explain it like I'm 10

    A class is a cookie cutter; objects are the cookies. One cutter, many cookies, and you can decorate each cookie differently.

    The technical explanation

    class Dog: starts a blueprint. rex = Dog() creates an object, also called an instance.

    You can attach data to an object with a dot: rex.name = "Rex". Next lesson shows the proper way, __init__.

    type(rex) shows the class and isinstance(rex, Dog) checks it.

    Syntax

    class Name:
        ...
    
    obj = Name()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    In Django every table is a class, and every row you load from the database becomes an object of that class.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is a class?
    2. 2. How do you create an object from class Car?
    3. 3. What is an instance?
    4. 4. Naming style for classes?
    5. 5. Do two objects of the same class share their attributes?

    Questions people ask

    What is the difference between a class and an object in Python?

    A class is the definition, like the plan for a house. An object is one actual house built from the plan. One class can create many objects.

    Is everything an object in Python?

    Yes. Numbers, strings, lists, functions and even classes are objects, which is why they all have methods you call with a dot.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between a class and an object in Python?

    A class is the definition, like the plan for a house. An object is one actual house built from the plan. One class can create many objects.

    beginner Is everything an object in Python?

    Yes. Numbers, strings, lists, functions and even classes are objects, which is why they all have methods you call with a dot.

    intermediate What is a class?

    A blueprint for objects. It describes a kind of thing.

    intermediate How do you create an object from class Car?

    Car(). Call the class.

    Quick revision

    Remember

    • Define a class
    • Create objects from it
    • Give each object its own data
    • Class names use CapitalWords (PascalCase): BankAccount, not bank_account. Objects and variables stay in snake_case.

    Syntax

    class Name:
        ...
    
    obj = Name()

    Official documentation