|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-methods/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/python-methods/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fpython-methods%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-class-object |
| Unlock This Lesson | https://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 Python | https://realpython.com/courses/python-class-object/ |
| Christopher Trudeau | https://realpython.com/courses/python-class-object/#team |
| Recommended Tutorial | https://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 Question | https://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 |
| Transcript | https://realpython.com/lessons/python-methods/#transcript |
| Discussion (5) | https://realpython.com/lessons/python-methods/#discussion |
| 00:00 | https://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:27 | https://realpython.com/lessons/python-methods/#t=27.12 |
| I told you I’d wait to repeat myself. Instance | https://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:41 | https://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 to | https://realpython.com/lessons/python-methods/#t=56.82 |
| what comes next. | https://realpython.com/lessons/python-methods/#t=61.0 |
| 01:03 | https://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:13 | https://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:25 | https://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 to | https://realpython.com/lessons/python-methods/#t=89.59 |
| maintain their respect and love. | https://realpython.com/lessons/python-methods/#t=94.8 |
| 01:36 | https://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 attributes | https://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:50 | https://realpython.com/lessons/python-methods/#t=110.39 |
| It’s an alternative to a constructor and usually is done if some sort of | https://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 you | https://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:09 | https://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:21 | https://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:38 | https://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:50 | https://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 a | https://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:09 | https://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 class | https://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 on | https://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:34 | https://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:49 | https://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 first | https://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:01 | https://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:16 | https://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 water | https://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 road | https://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 attributes | https://realpython.com/lessons/python-methods/#t=279.57 |
| 04:45 | https://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 static | https://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:05 | https://realpython.com/lessons/python-methods/#t=305.41 |
| This .all_float() method is a utility that checks if all the vehicles passed into it | https://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 the | https://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:28 | https://realpython.com/lessons/python-methods/#t=328.68 |
| It iterates over all the vehicles passed in and returns True if all of them | https://realpython.com/lessons/python-methods/#t=328.68 |
| float. Let’s create some vehicles. | https://realpython.com/lessons/python-methods/#t=333.63 |
| 05:38 | https://realpython.com/lessons/python-methods/#t=338.92 |
| First I import the class, | https://realpython.com/lessons/python-methods/#t=338.92 |
| 05:44 | https://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:55 | https://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:02 | https://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:13 | https://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 it | https://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:34 | https://realpython.com/lessons/python-methods/#t=394.55 |
| The volume is an instance method which bases its calculation on the dimensions | https://realpython.com/lessons/python-methods/#t=394.55 |
| tuple. Twelve thousand cubic microns | https://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:47 | https://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:08 | https://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:20 | https://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:35 | https://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:42 | https://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:59 | https://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:11 | https://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, 2023 | https://realpython.com/lessons/python-methods/#comment-b42566ba-6bd6-4e38-be9f-14f4c5c8262f |
| Sept. 23, 2023 | https://realpython.com/lessons/python-methods/#comment-734d612a-d4df-46d1-9ed8-c77709172fca |
| Dec. 31, 2024 | https://realpython.com/lessons/python-methods/#comment-05f324f5-a317-40cf-a373-d561538f0b21 |
| Dec. 31, 2024 | https://realpython.com/lessons/python-methods/#comment-95ec5990-7fda-404d-b43c-bb0b9ffed233 |
| Jan. 1, 2025 | https://realpython.com/lessons/python-methods/#comment-ef595d92-0d49-4f79-97c2-b2c86658cafb |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/python-properties-and-descriptors/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover