3

I have a class, for example:

class Example:

    def __init__(self):
        self.x = 1

    def add(self):
        self.x += 1
        return self

If I want to chain methods, I can use:

my_example = Example()
my = my_example.add().add().add().add().add().add()
# Use 'my' later on and modify and so on...

Will it consume more and more memory whenever I use add() or something ?

I want to build a library with method chaining functionality, but just wondering is this good ?

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57

1 Answers1

4

It won't consume more memory because self is a reference to the same object.

"Is this good?" - depends on the functionality you want. If you want to modify the existing object, then it should be fine.

ForceBru
  • 43,482
  • 10
  • 63
  • 98