+ 3

Can anyone explain simply what differences there are between functions and methods in Python?

Python Learning

26th May 2018, 8:29 AM
Saeed Mozaffari
Saeed Mozaffari - avatar
9 ответов
+ 2
The differences between Methods and Functions are that: Functions: Functions often takes values from an object and returns a value, but it don't change anything on the objects it took as it's arguments. Methods: Methods often take an object and an argument and use the argument to do something to the object. Examples: len Function returns the length of values put as it's arguments, but the values doesn't change anyways. append Method will add an item in the end of a list, then the list would have 1 more object always the method is executed.
26th May 2018, 12:09 PM
Seb TheS
Seb TheS - avatar
+ 12
methods are functions which belong to a class
26th May 2018, 1:30 PM
Adrien Godoy
Adrien Godoy - avatar
+ 7
python is not as strong oop as Java for example. So functions can live without a class as container. Their container is the prog. Methods are functions in a class .
26th May 2018, 8:54 AM
Oma Falk
Oma Falk - avatar
+ 5
Functions are pieces of reusable code. Methods are functions that belong to a class.
26th May 2018, 8:40 AM
Paul Grasser
Paul Grasser - avatar
+ 2
Talley Berry Thank you so much🙏🙏🙏
26th May 2018, 9:13 AM
Saeed Mozaffari
Saeed Mozaffari - avatar
+ 2
Seb TheS, very digestible explanations of "class , function , and method" . I don't know how i can thank you.
26th May 2018, 12:41 PM
Saeed Mozaffari
Saeed Mozaffari - avatar
+ 1
Paul Grasser Excuse me, what a class is?
26th May 2018, 8:51 AM
Saeed Mozaffari
Saeed Mozaffari - avatar
26th May 2018, 9:04 AM
Talley Berry
Talley Berry - avatar
+ 1
Class is a structure little similar to function definitions, that can contain values for different object having same attributes, class Myclass: def __init__(self, weight, height) self.weight = weight self.height = height A = Myclass("100 kg", "200 cm") print(A.weight) print(A.height) >>> 100 kg 200 cm >>> Class took 2 arguments "100 kg" for weight and "200 cm" for height, the variable which was assigned to Myclass("100 kg", "200 cm") is self : A = Myclass("100 kg", "200 cm") so A is self, when ever after assigning the class A.weight or A.height are executed, they will return "100 kg", which is weight and "200 cm", which is height. Also __init__ is an important part of creating class, it's a magic method. Classes and magic methods are explained on the 7th part of Python3 tutorials.
26th May 2018, 11:58 AM
Seb TheS
Seb TheS - avatar