What’s the difference between a method and a function

In Python, methods and functions are very similar but have one key difference: their relationship to a class.

The Main Difference

  • Functions are independent blocks of code that can be called by their name from anywhere in your program.
  • Methods are functions that are defined inside a class and are associated with an object or the class itself.
FeatureFunctionMethod
LocationDefined outside of classes.Defined inside a class.
Calling itCalled by its name: my_function().Called on an object or class: my_obj.my_method().
Data AccessOperates only on data passed to it.Can access and modify the data (attributes) held within the class.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *