Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Python, functions and methodsare both ways to package reusable code. However, there are a few specific circumstances where one might choose to use a function instead of a method:

  1. When the code being written does not need to be part of a class or object.

  2. When the code will be called by different objects or classes, and does not need to be tied to a specific object or class. For example, a function to calculate the average of a list could be used with any list of numbers, regardless of where the list comes from.

  3. When the code needs to be self-contained and operate on its own input. This is useful when the output from the function doesn’t depend on anything else in your code.

  4. When the code is not specific to a class or object, but is in fact an independent piece of standalone logic that can be called on its own.

Ultimately, the choice between using a function or a method depends on the specific needs of your code and the design of your program.