Python staticmethod and classmethod

preview_player
Показать описание
What are Python's staticmethod and classmethod?
Use coupon code MCODING at checkout for up to 91% off all yearly hosting plans!

More importantly, how do they work? What are they good for? Let's find out all about staticmethods and classmethods.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G, Pieter G, Krisztian M, Mutual Information, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Комментарии
Автор

One thing that probably ought to have been mentioned alongside class methods are class variables. If you're only accessing class variables with a method, it's probably better to make it a class method. Makes it clear what you're doing and prevents accidentally overwriting class variables with method ones. But class variables aren't all that common to begin with, so this is kind of niche. Still, this is the only major use case outside of alternate constructors (and maybe singlets?) I can think of.

QuantumHistorian
Автор

I've been working on a task that has to do with constructing a class instance with additional arguments and couldn't wrap my head around how to do that without breaking the existing API. This video couldn't have come more timely: now that I know that there's such thing as classmethod, I'll explore a bit more about that, and I think this will be handy in solving my problem. THANK YOU for the excellent content!

maxskoryk
Автор

I have been doing python for years now for research projects and uni homeworks. Basically been using with it almost all the time that I worked. I never got to learn these concepts tho despite trying a couple of times - all of the tutorials are either too basic or too specific to follow. This channel really fills that gap with flying colors, and I cannot stress enough how great of a find it is for me. Thanks, keep and up the good work!

pavelkovalenko
Автор

What a coincidence, I just learned about staticmethod the other day! Great video, great explanations. Thanks very much.

ciscoortega
Автор

I was just using staticmethods on my project the other day. I just like having the extra namespace for being more explicit 🙂 Cheers!

re.liable
Автор

One reason to use static method is that they are inherited and can be re-defined in sub-classes.

ADFsoft
Автор

These videos are amazing for intermediate level Python programmers and professional use. Keep making more!

lphillis
Автор

Really helpful to learn what the use cases might be !

mattlau
Автор

Becoming a Youtube Python legend, one video at a time.
Great work. I'm, so glad I found your channel in it's "early" stage.
I dig you'r thumbnails too. Simple, but class Y:.

JakubYTb
Автор

2:23 Been searching for a good explanation of why one typically uses classmethod instead of staticmethod for alternative constructors. This explained it perfectly. Thanks!

elefantsnablar
Автор

A random use case for static methods I found was when parallelising code. If I call an instance method, the whole instance object would be passed to the child process - a massive overhead that caused pretty much negated the parallelisation gains. A static method allowed passing of only the required attributes to process

reddragon
Автор

It wasn't mentioned, but I think another reason you might call a class method from an instance is if your instance was created from a factory based on an ABC or Protocol, so all of the classes have the same class method with potentially different content.
Although in that case maybe its still better to have an instance method which wraps the class method.

nigh_anxiety
Автор

Thank you for such a helpful video James!, much appreciated brother 👍🏼👏🏽

shashishekhar----
Автор

For the matrices, I think it's better to do this:
class Matrix:
...
def can_multiply(self, other):
...
and use a.can_multply(b). This way, you don't have to make a map from classes to functions or use things like type(a).__module__.can_multiply(a, b) to check if you can multiply two variables of unknown type.

orisphera
Автор

A good thing to realize is that editors like Pycharm indicate there is something wrong with a method when the method does not use any instance or class variables, and showing the message "could be static". This causes a lot of confusion for beginning programmers who then change all methods to @staticmethods.

alexd
Автор

Thanks for this. I have never used static methods myself and have sometimes wondered if I was missing an important use case. Guess not!

anthonyrogers
Автор

love the full and simple explanaition. i use staticmethods when appling MVC structure to my api's
from the user_model UserModel class i need only sertain methods from the UserModel class. so i can call whathever query method i need in my controllers.

marcgentner
Автор

It's just the arguments that change.

normal: self, *args
@classmethod: cls, *args
@staticmethod: *args
@property: self

RecursiveTriforce
Автор

Really great stuff thank you so much for making this

kevinz
Автор

My main use cases for static methods have been polymorphic work that doesn't need access to the cls or instance beyond variations in the type (rarest case), or serializer classes that use methods to (de)serialize a single field (i don't need the serializer class just the day passed in), or unit test classes

Aang