Functions and Methods

Remember that a function is a code block and that code blocks are collections of statements. Methods are similar to functions, and you define them the same way. The only difference is that, unlike functions, methods are associated with classes and are defined within them. Because of the similarities, the terms function and method will be used interchangeably in this section.

Step by Step: Defining and Calling a Method

To define, or declare, a function or method, you enter the keyword def followed by the function name, a list of arguments enclosed in parentheses, and finally a colon. Next come the statements, which make up the body of the function. They start on a new line and, like all code blocks, are indented (if you don't ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.