Purpose and functionality of Method vs function

I am trying to understand what is the difference between creating a method inside a class and creating a function. Both gives same output. Pls provide clarity

class Point:
    """ Create a new Point, at coordinates x, y """

    def __init__(self, x=0, y=0, z=0):
        """ Create a new point at x, y """
        self.x = x
        self.y = y
        self.z = ((self.x ** 2) + (self.y ** 2)) ** 0.5

and this function:

def distance(x,y):
    z=((x ** 2) + (y ** 2)) ** 0.5
    return z

both looks same to me

p = Point(3,4)
p.x, p.y, p.z

output: (3, 4, 5.0)



output: 5.0

Read more here: https://stackoverflow.com/questions/64404811/purpose-and-functionality-of-method-vs-function

Content Attribution

This content was originally published by Amol Gupta at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: