← Python Programming
Object-oriented Python and projects · 15 MIN

Classes and objects

Classes and objects

A class defines a new type; an object is an instance of that class. __init__ initializes instance attributes and self refers to the current instance. Keep classes focused and use functions when an object model adds unnecessary complexity.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

class Student:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hello, {self.name}"

print(Student("Ada").greet())