René's URL Explorer Experiment


Title: Class Methods (Video) – Real Python

Open Graph Title: Class Methods – Real Python

Description: In the previous lesson, you learned more about the cousin of attributes, properties. In this lesson, I’ll dive deeper on methods. There are three types of methods in Python: instance methods, those which you’ve seen so far; class methods, which are…

Open Graph Description: In the previous lesson, you learned more about the cousin of attributes, properties. In this lesson, I’ll dive deeper on methods. There are three types of methods in Python: instance methods, those which you’ve seen so far; class methods, which are…

Opengraph URL: https://realpython.com/lessons/python-methods/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Class Methods",
    "description": "In the previous lesson, you learned more about the cousin of attributes, properties. In this lesson, I’ll dive deeper on methods. There are three types of methods in Python: instance methods, those which you’ve seen so far\u003B class methods, which are…",
    "thumbnailUrl": ["https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg"],
    "uploadDate": "2023-09-05T14:00:00+00:00",
    "duration": "PT8M19S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/python-methods/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg
og:imagehttps://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/python-methods/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/python-methods/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fpython-methods%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-class-object
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-class-object
https://realpython.com/courses/python-class-object/#team
Class Concepts: Object-Oriented Programming in Pythonhttps://realpython.com/courses/python-class-object/
Christopher Trudeauhttps://realpython.com/courses/python-class-object/#team
Recommended Tutorialhttps://realpython.com/python-classes/
Course Slides (.pdf)https://realpython.com/courses/python-class-object/downloads/python-class-object-slides/
Sample Code (.zip)https://realpython.com/courses/python-class-object/downloads/python-class-object-code/
Ask a Questionhttps://realpython.com/lessons/python-methods/#discussion
https://realpython.com/feedback/survey/course/python-class-object/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/python-class-object/disliked/?from=lesson-title
Transcripthttps://realpython.com/lessons/python-methods/#transcript
Discussion (5)https://realpython.com/lessons/python-methods/#discussion
00:00https://realpython.com/lessons/python-methods/#t=0.54
In the previous lesson,https://realpython.com/lessons/python-methods/#t=0.54
you learned more about the cousin of attributes, properties. In this lesson,https://realpython.com/lessons/python-methods/#t=1.5
I’ll dive deeper on methods.https://realpython.com/lessons/python-methods/#t=5.99
There are three types of methods in Python: instance methods,https://realpython.com/lessons/python-methods/#t=8.73
those which you’ve seen so far; class methods,https://realpython.com/lessons/python-methods/#t=13.09
which are like class attributes,https://realpython.com/lessons/python-methods/#t=16.45
but more method-y; and static methods,https://realpython.com/lessons/python-methods/#t=18.38
which don’t require a reference to either a class or an object.https://realpython.com/lessons/python-methods/#t=21.65
00:27https://realpython.com/lessons/python-methods/#t=27.12
I told you I’d wait to repeat myself. Instancehttps://realpython.com/lessons/python-methods/#t=27.12
methods are those you’ve been using, and as their name implies,https://realpython.com/lessons/python-methods/#t=29.88
they operate on the instance object,https://realpython.com/lessons/python-methods/#t=34.71
which means there has to be an instance to be able to call them.https://realpython.com/lessons/python-methods/#t=37.17
00:41https://realpython.com/lessons/python-methods/#t=41.07
They’re typically used to do stuff to the data associated with the object,https://realpython.com/lessons/python-methods/#t=41.07
and while I’m repeating myself,https://realpython.com/lessons/python-methods/#t=45.78
each instance method must take at least one argument, a reference to the object,https://realpython.com/lessons/python-methods/#t=47.83
which by convention is named self. You know all this though,https://realpython.com/lessons/python-methods/#t=52.41
but I figured it’d be good to put it all in one place so you can compare it tohttps://realpython.com/lessons/python-methods/#t=56.82
what comes next.https://realpython.com/lessons/python-methods/#t=61.0
01:03https://realpython.com/lessons/python-methods/#t=63.32
A class method is associated with the class instead of the instance.https://realpython.com/lessons/python-methods/#t=63.32
To indicate that it’s a class method,https://realpython.com/lessons/python-methods/#t=68.46
you wrap it with the @classmethod decorator.https://realpython.com/lessons/python-methods/#t=70.3
01:13https://realpython.com/lessons/python-methods/#t=73.59
This kind of method also requires at least one argument,https://realpython.com/lessons/python-methods/#t=73.59
but instead of it being the object,https://realpython.com/lessons/python-methods/#t=77.1
it’s a reference to the class. Like self,https://realpython.com/lessons/python-methods/#t=78.99
there’s a convention for this one as well.https://realpython.com/lessons/python-methods/#t=82.47
01:25https://realpython.com/lessons/python-methods/#t=85.22
It should be named class, spelled cls, and like self,https://realpython.com/lessons/python-methods/#t=85.22
this is only enforced through the fear of your fellow coders—or tohttps://realpython.com/lessons/python-methods/#t=89.59
maintain their respect and love.https://realpython.com/lessons/python-methods/#t=94.8
01:36https://realpython.com/lessons/python-methods/#t=96.15
That sounds a little more positive than a threat.https://realpython.com/lessons/python-methods/#t=96.15
A couple common uses for class methods are manipulating class attributeshttps://realpython.com/lessons/python-methods/#t=99.28
and factories.https://realpython.com/lessons/python-methods/#t=104.74
A factory is a method that returns an instance of a new object.https://realpython.com/lessons/python-methods/#t=106.44
01:50https://realpython.com/lessons/python-methods/#t=110.39
It’s an alternative to a constructor and usually is done if some sort ofhttps://realpython.com/lessons/python-methods/#t=110.39
side effect needs to be achieved when creating the object. For example,https://realpython.com/lessons/python-methods/#t=115.11
say your class has a couple of configurations of arguments where if youhttps://realpython.com/lessons/python-methods/#t=119.42
specified one, you had to use its companion, but if you specified another,https://realpython.com/lessons/python-methods/#t=123.32
it had to be paired with something else.https://realpython.com/lessons/python-methods/#t=127.14
02:09https://realpython.com/lessons/python-methods/#t=129.5
You could enforce this inside the .__init__(),https://realpython.com/lessons/python-methods/#t=129.5
or you could provide two factory methods with the required arguments.https://realpython.com/lessons/python-methods/#t=132.78
Granted, both these cases are probably a sign that your code is too complicated.https://realpython.com/lessons/python-methods/#t=137.39
02:21https://realpython.com/lessons/python-methods/#t=141.71
Part three of this course talks about the why of object-oriented design. For now,https://realpython.com/lessons/python-methods/#t=141.71
I’ll stick with the how. Finally (all right,https://realpython.com/lessons/python-methods/#t=146.03
I didn’t mean that as programmer pun, but I’m going to leave it right there),https://realpython.com/lessons/python-methods/#t=150.46
you have static methods. Instance methods need an object.https://realpython.com/lessons/python-methods/#t=153.96
02:38https://realpython.com/lessons/python-methods/#t=158.31
Class methods need a reference to the class.https://realpython.com/lessons/python-methods/#t=158.31
Static methods are distinguished by requiring neither of those things.https://realpython.com/lessons/python-methods/#t=160.76
Like with class methods, you create this using a decorator.https://realpython.com/lessons/python-methods/#t=165.01
02:50https://realpython.com/lessons/python-methods/#t=170.08
It can take arguments, but none are required.https://realpython.com/lessons/python-methods/#t=170.08
They’re typically used to group data-less functions together. For example,https://realpython.com/lessons/python-methods/#t=173.61
kilometers-to-miles and liters-to-gallons conversions, both inside of ahttps://realpython.com/lessons/python-methods/#t=178.27
converter class. Of course,https://realpython.com/lessons/python-methods/#t=183.2
this kind of grouping can be done equally well with a module.https://realpython.com/lessons/python-methods/#t=185.51
03:09https://realpython.com/lessons/python-methods/#t=189.9
I’m honestly not sure why static methods are included in Python.https://realpython.com/lessons/python-methods/#t=189.9
I suspect it’s because they’re there in other object-oriented languages.https://realpython.com/lessons/python-methods/#t=194.08
Any use case I can think of can be done equally well with a module or a classhttps://realpython.com/lessons/python-methods/#t=198.66
method. For example, if I recall correctly,https://realpython.com/lessons/python-methods/#t=202.88
all the math functions are grouped together as static methods in JavaScript onhttps://realpython.com/lessons/python-methods/#t=205.94
the Math class. In Python, they’re in the math module instead.https://realpython.com/lessons/python-methods/#t=209.8
03:34https://realpython.com/lessons/python-methods/#t=214.85
This isn’t one I tend to use myself, but let’s go look at some examples.https://realpython.com/lessons/python-methods/#t=214.85
Here, I have a Vehicle class. First,https://realpython.com/lessons/python-methods/#t=221.06
let’s look at the .water_vehicle() factory, which is a class method.https://realpython.com/lessons/python-methods/#t=224.4
03:49https://realpython.com/lessons/python-methods/#t=229.47
I indicate that with the @classmethod decorator.https://realpython.com/lessons/python-methods/#t=229.47
Notice the use of cls, pronounced class, as the firsthttps://realpython.com/lessons/python-methods/#t=232.41
argument, the class method’s equivalent of self.https://realpython.com/lessons/python-methods/#t=237.23
04:01https://realpython.com/lessons/python-methods/#t=241.45
As this is intended as a factory, its job is to construct a Vehicle object.https://realpython.com/lessons/python-methods/#t=241.45
So that’s the first thing it does.https://realpython.com/lessons/python-methods/#t=245.81
And then I set some attributes, and then because it is a factory,https://realpython.com/lessons/python-methods/#t=248.21
which is like a constructor, I return it.https://realpython.com/lessons/python-methods/#t=252.6
04:16https://realpython.com/lessons/python-methods/#t=256.09
And why might I do this instead of say just using a .__init__()?https://realpython.com/lessons/python-methods/#t=256.09
Well,https://realpython.com/lessons/python-methods/#t=261.18
this ensures that the name and dimensions are required when constructing a waterhttps://realpython.com/lessons/python-methods/#t=261.58
vehicle. By contrast,https://realpython.com/lessons/python-methods/#t=265.75
the name, dimensions, and number of wheels is required when creating a roadhttps://realpython.com/lessons/python-methods/#t=268.61
vehicle. This one’s also a factory,https://realpython.com/lessons/python-methods/#t=273.03
but with different arguments. Like the water vehicle,https://realpython.com/lessons/python-methods/#t=276.21
it constructs a new Vehicle object and sets its attributeshttps://realpython.com/lessons/python-methods/#t=279.57
04:45https://realpython.com/lessons/python-methods/#t=285.79
and then returns it. Nothing new here.https://realpython.com/lessons/python-methods/#t=285.79
This is a regular old instance method, taking self as an argument.https://realpython.com/lessons/python-methods/#t=290.47
And here is a static method. Because it is a statichttps://realpython.com/lessons/python-methods/#t=295.86
method, it doesn’t have self or cls as an argument.https://realpython.com/lessons/python-methods/#t=300.7
05:05https://realpython.com/lessons/python-methods/#t=305.41
This .all_float() method is a utility that checks if all the vehicles passed into ithttps://realpython.com/lessons/python-methods/#t=305.41
float. If you haven’t seen the star notation here before,https://realpython.com/lessons/python-methods/#t=310.63
it means the method can take any number of arguments. Inside the method,https://realpython.com/lessons/python-methods/#t=315.2
the vehicle argument is used as an iterable to iterate over all thehttps://realpython.com/lessons/python-methods/#t=320.0
arguments passed in, and that’s actually what this method does.https://realpython.com/lessons/python-methods/#t=324.78
05:28https://realpython.com/lessons/python-methods/#t=328.68
It iterates over all the vehicles passed in and returns True if all of themhttps://realpython.com/lessons/python-methods/#t=328.68
float. Let’s create some vehicles.https://realpython.com/lessons/python-methods/#t=333.63
05:38https://realpython.com/lessons/python-methods/#t=338.92
First I import the class,https://realpython.com/lessons/python-methods/#t=338.92
05:44https://realpython.com/lessons/python-methods/#t=344.11
and here I’ve used the .water_vehicle() factory,https://realpython.com/lessons/python-methods/#t=344.11
which is a class method, to create a new vehicle.https://realpython.com/lessons/python-methods/#t=346.78
The .water_vehicle() factory requires a name for the vehicle and some dimensions.https://realpython.com/lessons/python-methods/#t=350.47
05:55https://realpython.com/lessons/python-methods/#t=355.32
Instead of light-years, let’s say these are microns. It’s a very tiny boat.https://realpython.com/lessons/python-methods/#t=355.32
06:02https://realpython.com/lessons/python-methods/#t=362.09
The factory called the constructor, instantiated the object, and returned it,https://realpython.com/lessons/python-methods/#t=362.09
so I can now use the boat object and see its attributes like any other.https://realpython.com/lessons/python-methods/#t=366.29
06:13https://realpython.com/lessons/python-methods/#t=373.61
The factory set other values as well, so not just those passed in.https://realpython.com/lessons/python-methods/#t=373.61
Boats don’t have wheels. Steering wheels don’t count.https://realpython.com/lessons/python-methods/#t=378.2
So rather than a default argument value and the chance that someone will set ithttps://realpython.com/lessons/python-methods/#t=381.65
in .__init__(), the factory pattern makes sure here that it is zero.https://realpython.com/lessons/python-methods/#t=386.49
06:34https://realpython.com/lessons/python-methods/#t=394.55
The volume is an instance method which bases its calculation on the dimensionshttps://realpython.com/lessons/python-methods/#t=394.55
tuple. Twelve thousand cubic micronshttps://realpython.com/lessons/python-methods/#t=398.73
it is. Let’s create another vehicle.https://realpython.com/lessons/python-methods/#t=401.39
06:47https://realpython.com/lessons/python-methods/#t=407.08
This time, I’ve used the other factory, also a class method.https://realpython.com/lessons/python-methods/#t=407.08
This factory takes three arguments: the name, the dimension tuple,https://realpython.com/lessons/python-methods/#t=410.72
and the number of wheels. The method itself, of course, takes four,https://realpython.com/lessons/python-methods/#t=414.23
the first argument being the class, but when you’re invoking it here like I did,https://realpython.com/lessons/python-methods/#t=418.15
you don’t talk about that. There’s the name …https://realpython.com/lessons/python-methods/#t=422.39
07:08https://realpython.com/lessons/python-methods/#t=428.72
and the number of wheels and its volume.https://realpython.com/lessons/python-methods/#t=428.72
Now let’s use the static method.https://realpython.com/lessons/python-methods/#t=435.05
07:20https://realpython.com/lessons/python-methods/#t=440.12
You call static methods like you call class methods, on the class itself.https://realpython.com/lessons/python-methods/#t=440.12
Static methods don’t have a class or object passed to them though,https://realpython.com/lessons/python-methods/#t=446.28
so it can only deal with the arguments passed into the method.https://realpython.com/lessons/python-methods/#t=450.52
07:35https://realpython.com/lessons/python-methods/#t=455.13
Passing in this boat returns True, as boats can float.https://realpython.com/lessons/python-methods/#t=455.13
07:42https://realpython.com/lessons/python-methods/#t=462.99
I can pass the boat in twice. Remember, the algorithm doesn’t care.https://realpython.com/lessons/python-methods/#t=462.99
It doesn’t consider that it’s the same thing twice.https://realpython.com/lessons/python-methods/#t=466.77
It just iterates through all the arguments and checks if each can float.https://realpython.com/lessons/python-methods/#t=469.17
07:59https://realpython.com/lessons/python-methods/#t=479.49
Adding the car to the call tells me something in the argument list can’t float.https://realpython.com/lessons/python-methods/#t=479.49
It’s not entirely fair. Cars can float, just not for very long.https://realpython.com/lessons/python-methods/#t=484.42
08:11https://realpython.com/lessons/python-methods/#t=491.02
You’ve seen a lot of little bits and pieces of sample code.https://realpython.com/lessons/python-methods/#t=491.02
It’s time to put it all together into a working example.https://realpython.com/lessons/python-methods/#t=494.75
Sept. 22, 2023https://realpython.com/lessons/python-methods/#comment-b42566ba-6bd6-4e38-be9f-14f4c5c8262f
Sept. 23, 2023https://realpython.com/lessons/python-methods/#comment-734d612a-d4df-46d1-9ed8-c77709172fca
Dec. 31, 2024https://realpython.com/lessons/python-methods/#comment-05f324f5-a317-40cf-a373-d561538f0b21
Dec. 31, 2024https://realpython.com/lessons/python-methods/#comment-95ec5990-7fda-404d-b43c-bb0b9ffed233
Jan. 1, 2025https://realpython.com/lessons/python-methods/#comment-ef595d92-0d49-4f79-97c2-b2c86658cafb
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/python-properties-and-descriptors/
Overviewhttps://realpython.com/courses/python-class-object/
https://realpython.com/lessons/python-class-object-review/
Class Concepts: Object-Oriented Programming in Python (Overview) 03:22 https://realpython.com/videos/python-class-object-overview/
The Case for Object-Oriented Programming 09:55 https://realpython.com/videos/why-oop-python/
Class Creation 09:43 https://realpython.com/lessons/python-class-creation/
Attributes 06:44 https://realpython.com/lessons/python-attributes/
Properties and Descriptors 07:51 https://realpython.com/lessons/python-properties-and-descriptors/
Class Methods 08:19 https://realpython.com/lessons/python-methods/
A Complete Example: Putting It All Together 10:22 https://realpython.com/lessons/python-class-object-review/
Class Concepts: Object-Oriented Programming in Python (Summary) 04:34 https://realpython.com/lessons/python-class-object-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.