2

From my understanding, methods are functions inside classes that act on the object itself. But what if there's a certain function (that does NOT act on the object itself) I want to call inside a class?

I've done some testings, and apparently both classes down below do the same thing. Which one is the best, though, Class1 that defines the function as a method that does not act on the object, or Class2 that defines the function inside the method itself, so only that specific method can use it?

class Class1:
    def __init__(self):
        self.test=0

    def method(self):
        self.test=Class1.function(x)

    def function(x):
        #obviosly simple because it's just an example
        return x**2 

class Class2:
    def __init__(self):
        self.test=0

    def method(self):
        def function(x):
            #obviosly simple because it's just an example
            return x**2
        self.test=function(x)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
ikki
  • 21
  • 1

0 Answers0