Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods

preview_player
Показать описание

What's the difference between @classmethod, @staticmethod, and "plain/regular" instance methods in Python? You'll know the answer after watching this Python video tutorial:

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 video tutorial I go over the differences between these different kinds of methods in Python. I also explain when to use each with a simple example so you can improve your object-oriented programming (OOP) skills in Python.

To learn how to use the full potential of Python check out "Python Tricks: The Book" at the link below.

* * *

FREE Python Tutorials & News:
Рекомендации по теме
Комментарии
Автор

Hi Dan! Italians are watching and approve your spelling! Keep up the good work!

teoguso
Автор

You explained that very simply in less than 15 minutes. You are a wizard.

meiamavice
Автор

Dude - these tutorials are freakin' awesome. Keep it up. I am learning all sorts of stuff. And we really appreciate them!

IcarianVX
Автор

Dan, a Margherita pizza has 3 ingredients besides the dough representing the Italian flag colors.
Red Tomatoes, White Buffalo Mozzarella, and Green Fresh Basil.
Great series on Pyrhon!

nox
Автор

I know these are a little older but having done some course with others, I find yours very organised. The choice of examples are very good, perhaps because you have taken time to write a book about it. It does show. You can I am on the play list till the last course. Thank you

BijouBakson
Автор

"This isn't about cheese, this is about Python"

Fantastic. Great tutorial, Mr. Bader!

erichanko
Автор

I don't really agree with your static methods can't modify state claim. See below:

class Pizza:
__some_state = 0

@staticmethod
def _static_foo():
Pizza.__some_state += 1

If I am not mistaken, @staticmethod came first but was insufficient because it didn't deal with inheritance well. We got @classmethod to deal with this shortcoming. I really don't see the need for @staticmethod anymore and I believe it was only left for backwards compatibility.

skewty
Автор

Ive come to this series in 2019 and it looks absolutely FANTASTIC thanks for sharing this with the world mate!

irateindividual
Автор

This was actually a really great explanation and a good prosciutto spelling. Thank you!

alexkalopsia
Автор

great examples for class methods. I never really find a use for them but might have to rethink.

murtagh
Автор

Very nice explanation. There are some basic things that we skip and forget from time to time. Great video(s) Dan. Cheers!

tfsoa
Автор

Great work on explaining these three concepts! Keep it up! 👍

adreanramires
Автор

One thing that id love to see answered is why use a class method over a static method? Do you really need access to cls to instantiate the class? You can return an instance of the class from a static method, so what is the real benefit of the class method??

joe_of_all_things
Автор

A good use of static methods is to create a utility class, ie MyStringUtils, which would contain only static methods that provide custom methods for calculations or manipulations for a particular type of item.

francismcguire
Автор

Hi. there. My name is Randy. I really appreciate your efforts in delivering all those high quality tutorials, I benefit from it a lot. I'd would really want other people in China to know your channel too. Unfortunately, the GFW blocks us from visiting website like Youtube, Facebook, twtiter, etc. With your permission, I would really like to share your videos and translate them into Chinese then put them on a Chinese Website. If, by any chance, you don't mind, please let me know.

randixlai
Автор

love that idle. what is it? the box with autocompletion is gorgeous.

ulfgj
Автор

@RealPython what do you think about it?:

class A:
x = 1

@staticmethod
def change_static():
A.x = 2

@classmethod
def change_class(cls):
cls.x = 3


>>> A.x
1
>>> A.change_static()
>>> A.x
2
>>> A.change_class()
>>> A.x
3

TechMarketer
Автор

Really superb teaching.
Learnt @classmethod and
@staticmethod

Thanks for this wonderful video.

Ranjithkumar-hdol
Автор

bewitching one mate i'm looking forward to watch all the concepts from ur channel, do u also update ur videos in udemy regarding framework, appium with python actually m looking forward to buy ur course.

sundarambhardwaj
Автор

Yes! Was looking for a video like this

BrendanMetcalfe