Functions vs. methods

hey. can someone help me understand the differences in functions and methods in python, if there are any?

it seems to me that functions happen before the element and methods come after.

len function = len(list)
append method = list.append(x)

is this correct?

is a method an argument from another function somewhere?

it seems these words keep getting used interchangeably in the lessons. causing me some confusion.

thanks for the help!

1 Like

just googled it and watched a couple videos.

i haven’t gotten to “classes” yet in the lessons.

so when you create a class, you can create multiple functions within that class. then you can call that function elsewhere as a method. Is this correct?

2 Likes

Hello @shaunbarbour,

Not necessarily. You can have methods (like zip()) which come before. I use method for built-in ones(functions, or code that modifies something and returns that output), or ones in classes I have created. Functions are more ones I create, outside of classes.

Yes, that is correct! That is why if you try to do a method on an object(class) that doesn’t have that method, it throws an error.
I hope this helps!

1 Like

+1 for Googling your own question and asking for further clarification. Yes, methods are essentially functions belonging to a class. You can call that function (method) on any instance of that class. However, you can’t call it on a different class unless that class has the same method.

3 Likes

thanks so much! making more sense.

really appreciate that. you explanation helps the most. simple. thanks!

If you want a bit more of an in-depth look into why some methods have Obj.method(), as in why they have a . then method, look below (you should cover this in classes, but this might give you a little head start):
When you create a class with methods, you are creating an object. That object then has methods that can be used to alter the object you’ve created. The same goes with built-in methods. They were on specific objects (which are basically the same as the objects you create with classes, just built into the language). That means you can use them to alter specific aspects of the object (remember, most things are objects; strings, integers, etc). Take the .append() method:

#The .append() method adds-puts-the given parameters onto the end of the string
a = ["a", "b", "c"]
a.append("d")#The new list is now ["a","b","c","d"]
#This method operates on an object (list), and does a certain thing
#to it (adds elements to the end).

This article also provides a nice explanation.
I hope this was interesting!

2 Likes

good stuff. thanks so much for the help and your time. very helpful.

1 Like

one more thing…

class Pet(object):
   def my_method(self):
      print("I am a Cat")
cat = Pet()
cat.my_method()

.my_method… what is the object it is called on? cat? if so, what type of object is cat? how would i use that method practically?

the built in method of .append is called on list objects, correct?

how would i create a class and method that can be called on any string, to say, for example, to remove the last word from the string?

and can it be called outside of the class? like a class and methods are created, then can be called in the general program script?

i’m sure these questions will be covered when i get to classes in the lessons, so feel free to not answer and tell me to wait. lol.

You should probably wait until you learn more about classes, since we can’t really put everything you need to know about OOP into a comment.

That being said, I’d just like to remind you that a method is just a function that is specific to a class. Don’t overthink it. If you need the function to work on something other than a certain class then just write a function.

2 Likes

got it. will do. thanks!

You did this to define the cat variable:

cat = Pet()

Thereafter, cat refers to an object of type Pet.

You can try this:

print(type(cat))
2 Likes

makes sense. thanks much.

1 Like