[Python] Refactoring a stateful Python class best practice

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

  1. Stack

    Stack Membro Participativo

    I have a fairly large python class that follows this structure:

    class A:
    def __init__(self):
    self.a = 0
    self.b = 0

    def foo(self):
    self.a = 1

    def bar(self):
    self.b = 2


    I want to refactor the methods in this class into different files. What is the best way to refactor the method foo into another file? Is this a common practice?

    class Foo:
    def __init__(self, owner):
    self.owner = owner

    def foo(self):
    self.owner.a = 1

    class A:
    def __init__(self):
    self.a = 0
    self.b = 0
    self.foo = Foo(self)

    def bar(self):
    self.b = 2

    Continue reading...

Compartilhe esta Página