Tuesday, June 11, 2019

OOP Method Types in Python: @classmethod vs @staticmethod vs Instance Methods

What’s the difference between @classmethod, @staticmethod, and “plain/regular” instance methods in Python? You’ll know the answer after watching this video course:

  • Regular (instance) methods need a class instance and can access the instance through self. They can read and modify an objects state freely.

  • Class methods, marked with the @classmethod decorator, don’t need a class instance. They can’t access the instance (self) but they have access to the class itself via cls.

  • Static methods, marked with the @staticmethod decorator, don’t have access to cls or self. They work like regular functions but belong to the class’s namespace.

In this course you’ll go over the differences between these three kinds of methods in Python. You’ll also see when to use each with a simple example, so you can improve your object-oriented programming (OOP) skills in Python.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...