9 Antworten
+ 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.
+ 12
methods are functions which belong to a class
+ 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 .
+ 5
Functions are pieces of reusable code. Methods are functions that belong to a class.
+ 2
Talley Berry Thank you so much🙏🙏🙏
+ 2
Seb TheS, very digestible explanations of "class , function , and method" . I don't know how i can thank you.
+ 1
Paul Grasser Excuse me, what a class is?
+ 1
BEN10MZF check out this lesson: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2467/
+ 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.