POST 1 of 5 MorningPythonConcept
What a Python class actually is
Week 2 starts with a topic most people make harder than it needs to be — Python classes. A class is a factory that produces objects with shared behaviour. That's the whole concept. Stripped of the formalism, you're just saying 'here's a recipe for making things; each thing has its own data and shares the same methods'. The one piece that confuses everyone is self. Self isn't magic, isn't a keyword, isn't enforced by the runtime. It's just the name we conventionally give to the first parameter of every method, and Python passes the instance into that slot when you call obj.method(). 'def greet(self):' is just shorthand for 'def greet(instance_of_this_class):'. You could call it 'this', or 'me', or 'me_obj'. The convention is self, and following it is non-negotiable in any team codebase. But knowing why it's there demystifies the whole class system. The rest of OOP in Python rests on three things. __init__ — the constructor. Runs once when you create an object. Use it to set the instance's initial attributes (self.x = x, self.y = y). Attributes — data stored on the instance. Accessed via self.x inside methods, obj.x outside. Type hints make them readable. Methods — functions defined in the class body that take self as the first argument. Bound to the class so every instance can call them. That's 90% of OOP in Python. The remaining 10% is conventions — how to name private attributes (single underscore prefix), when to use class methods versus static methods (rarely), how to override dunder methods to integrate with built-ins (tomorrow's topic). Don't overengineer. Most useful classes are 30 lines and three methods.
#Python#AI#100DaysOfCode#BuildInPublic#PythonProgramming#OOP