liveBook · Manning

Method

As explained in A Pythonic Adventure: From Python basics to a working web app

A method is a function associated with an object and invoked through that object.

A method is a function associated with an object. Call it with the object's name, a dot, the method name, and parentheses: object.method(). Methods can return values, just like functions. section 5.0

Reading a menu file

After opening drinks.txt, the file object f can use its read() method. Store the returned file content, then print it: f = open("drinks.txt") drinks = f.read() print(drinks) The result contains the file's contents, including coffee, chocolate, and decaf. section 5.0

f = open("drinks.txt")
drinks = f.read()
print(drinks)

Inspect the complete three-line example that opens a file, calls read(), stores its result, and prints it. section 5.0

YOUR TURN

  • Write the short program yourself, read drinks.txt, and print the whole file. section 5.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Defines method syntax and return values through read(), while deferring a fuller discussion of objects.
Examples
Demonstrates f.read() on drinks.txt and asks learners to reproduce the file-reading program.
What it assumes
Assumes familiarity with function-like calls but introduces object methods and dot notation from the beginning.
What you can do after
Apply Python's read() method to retrieve and print a file's contents.
How it connects
Ties methods to functions, objects, file objects, and return values.

Also explained in

Compare how 20 books explain method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.