[Python] Importance of putting self in a function definition in Python?

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    Let's assume that you have the following:

    class Solution1(object):
    def some_function(self, a, b):
    self.ans = 0
    def some_other_function(x, y):
    manipulates self.ans
    return self.ans


    class Solution2(object):
    def some_function(self, a, b):
    ans = 0
    def some_other_function(x, y):
    manipulates ans
    return ans


    the only difference between Solution1 and Solution2 are that one has self.ans and one has ans as variables.

    Can they differ in values?

    Continue reading...

Compartilhe esta Página