Formation
90 min read
Object-Oriented Programming
π¦ Classes and Objects (OOP)
Learning objectives
π― Objectives:\n
1Understand OOP\n2. Create classes\n3. Create objects\n4. Use __init__\n5. Understand self
Introduction
π Object-Oriented Programming (OOP) organizes code around objects.
Theoretical content
Class and object:\n
PYTHON
\nclass Person:\n def __init__(self, name):\n self.name = name\nPractical examples
π» Example: BankAccount class\n
PYTHON
\nclass BankAccount:\n def __init__(self, owner):\n self.owner = owner\nBest practices
1PascalCase for class names\nβ
2. Use properties (@property)\nβ
3. Encapsulate data\nβ
4. Use __str__ for representation
Common pitfalls
Forgetting self in methods\n
self is always the first parameter
Summary
Classes: blueprints\nβ
Objects: instances\nβ
Attributes: data\nβ
Methods: behaviors\nβ
__init__: constructor\nβ
self: reference to instance
Additional resources
π docs.python.org/3/tutorial/classes.html