Python Methods

A method is a function that is inside a class.  It is appended to an object unlike functions which just run.  They look a lot alike and the differences are subtle at first and then become more obvious.  The simplest way I can say this is the methods appends an object.

“print(x)” is a function

“x.my_method()” is a method

Now that is oversimplifying it, but it that is all you take away that is fine.  Now we will get a little more detailed.  Here is a function and a method.  They are both called and do basically the same thing, print some text.  However, the method needs to be called on an object as opposed to a function which can just be run.

#I am a function
def my_function():
    print("I was printed in a function")


class Things:
    #I am a method
    def my_method(self):
        print("I was printed in a method.")


my_function()  #Function does not need an object

t = Things()
t.my_method()  # Method needs an object

If you are more confused than before, do not worry just keep using methods and functions and eventually it will make sense.