Tuesday Tip #50: What is a "method" in Python? 🐍


Hi Reader,

Next week, I’ll be offering a Black Friday sale on ALL of my courses.

I’ll send you the details this Thursday! 🚨


👉 Tip #50: What is a "method" in Python?

In Python, a method is a function that can be used on an object because of the object's type.

For example, if you create a Python list, the "append" method can be used on that list. All lists have an "append" method simply because they are lists:

If you create a Python string, the "upper" method can be used on that string simply because it's a string:

To be clear, my_list doesn't have an "upper" method and my_string doesn't have an "append" method. That's because methods only exist for a particular object if they have been explicitly defined for that type of object.


☎️ Calling a method

To call a method, the format is object_name.method_name(), and any arguments to the method are listed within the parentheses.

The method implicitly acts on the object being named, and thus some methods don't have any stated arguments since the object itself is the only necessary argument. For example, my_string.upper() doesn't have any listed arguments because the only required argument is the object itself, my_string.


📓 What about modules?

One common point of confusion relates to the following:

Is "sqrt" a method of the "math" object? No. Instead, this is how you call the "sqrt" function from the "math" module.

The format being used here is module_name.function_name() instead of object_name.method_name().

You can distinguish between the two formats by examining the imports to see whether or not the part before the period ("math", "my_list", "my_string") was imported as a module.


🐍 Want to level up your Python skills?

Whether you're new to Python or just want to fill in the gaps in your knowledge, I'd invite you to join 500+ happy students in my signature Python course, Python Essentials for Data Scientists.


👋 See you on Thursday!

In two days, I'll be sending you the details of my Black Friday sale! 😄

- Kevin

P.S. Playing a prank on Google Street View

Did someone AWESOME forward you this email? Sign up here to receive more Data Science tips!

Learn Artificial Intelligence from Data School 🤖

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School 🤖

Hi Reader, Here are your top AI stories for the week: AI-driven education in 2 hours per day Add yourself to an AI-generated TV show AI models send "subliminal messages" to one another Read more below! 👇 Sponsored by: Superhuman AI Find out why 1M+ professionals read Superhuman AI daily. AI won't take over the world. People who know how to use AI will. Here's how to stay ahead with AI: Sign up for Superhuman AI. The AI newsletter read by 1M+ pros. Master AI tools, tutorials, and news in just...

Hi Reader, In this week’s tip, I’ll be breaking down some highly practical advice for taking full advantage of the capabilities of today’s AI models. Check it out below! 👇 Today’s tip is based on Ethan Mollick’s excellent article, Using AI Right Now: A Quick Guide. I recommend reading the whole thing if you have time, but if not, I’ve pulled out some important quotes from the article and added my own commentary: For most people who want to use AI seriously, you should pick one of three...

Hi Reader, Here are the most important AI stories I’ve found this month: Microsoft’s AI solves medical mysteries Cloudflare charges AI crawlers Books are legal for AI training AI model teaches itself AI researchers are paid like superstars Details below! 👇 🩺 Microsoft’s AI solves medical mysteries Microsoft’s new “AI Diagnostic Orchestrator” solved 85% of 304 real medical mysteries, whereas experienced doctors (who did not have access to colleagues or medical databases) only solved an average...