Writing object-oriented programs
As we said in the first section of this chapter, Python is a multi-paradigm language, and one of those paradigms is object-oriented programming. In this section, we’ll review how you can define classes and how you can instantiate and use objects. You’ll see that Python syntax is once again very lightweight.
Defining a class
Defining a class in Python is straightforward: use the class
keyword, type the name of your class, and begin a new block. You can then define methods under it just like you would for regular functions. Let’s review an example:
chapter02_classes_objects_01.py
class Greetings: def greet(self, name): return f"Hello, {name}" c = Greetings() print(c.greet("John")) # "Hello, John"