Sahina Rayeesa’s Post

🧠 Python Concept That Powers @property & Methods: __set_name__ Hidden hook during class creation 👀 🤔 What Is __set_name__? When a class is created, Python tells each descriptor: 👉 “Hey, your attribute name is x.” That hook is __set_name__. 🧪 Example class Field: def __set_name__(self, owner, name): self.name = name def __get__(self, instance, owner): return instance.__dict__.get(self.name) def __set__(self, instance, value): instance.__dict__[self.name] = value class User: age = Field() name = Field() u = User() u.age = 20 print(u.age) The descriptor automatically knows its field name 🎯 🧒 Simple Explanation Imagine giving kids name badges 🏷️ The teacher tells each kid: 👉 “Your name is Asha.” That’s __set_name__. 💡 Why This Is Powerful ✔ Self-aware descriptors ✔ ORM-like fields ✔ Framework internals ✔ Cleaner reusable components ⚡ Real-World Use 💻 Django models 💻 ORMs 💻 Validation frameworks 💻 Data descriptors 🐍 Python classes don’t just define attributes. 🐍 They introduce them by name 🐍 __set_name__ is one of those hooks you never see — but frameworks rely on it. #Python #PythonTips #PythonTricks #AdvancedPython #CleanCode #LearnPython #Programming #DeveloperLife #DailyCoding #100DaysOfCode

  • text

To view or add a comment, sign in

Explore content categories