What is the output of the following code? class Chainable: def __init__(self, value): self.value = value def add(self, n): self.value += n return self def mul(self, n): self.value *= n return self result = Chainable(2).add(3).mul(4).value print(result)

Python Professional Hard

Python Professional — Hard

What is the output of the following code? class Chainable: def __init__(self, value): self.value = value def add(self, n): self.value += n return self def mul(self, n): self.value *= n return self result = Chainable(2).add(3).mul(4).value print(result)

Key points

  • Chainable class allows method chaining for adding and multiplying values
  • The final value is obtained by performing operations in sequence
  • The value after adding 3 is multiplied by 4 to get the final result

Ready to go further?

Related questions