PY-101 ยท Object-oriented Python
Classes and objects
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
class Dog:passrex = Dog()rex.name = "Rex"bella = Dog()bella.name = "Bella"print(rex.name, bella.name)print(type(rex).__name__, isinstance(bella, Dog))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Rex Bella
Dog True ๐ฏ Create a class Book with no body (pass). Make two Book objects, give each a title attribute, and print both titles on one line: Gita Wings
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
class Book: pass; a = Book(); a.title = "Gita"; ...
- 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.
- The Python idea
class Name:A class is a blueprint that describes a kind of thing, and an object is one actual thing made from that blueprint. - 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() Real-world scenario
In Django every table is a class, and every row you load from the database becomes an object of that class.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: __init__ and 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, notbank_account. Objects and variables stay in snake_case.
Syntax
class Name:
...
obj = Name() Official documentation
- Classes (docs.python.org)