Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling chains methods with intermediate results

I have a class and some methods of it. Could I keep a result of the methods between calling.

Example calling:

result = my_obj.method_1(...).method_2(...).method_3(...)

when method_v3(...) received a result from the method_2(..) who received a result from the method 1(..)

Please tell me, are there patterns or anything to decide above example?

like image 614
Abbasov Alexander Avatar asked Jun 26 '13 13:06

Abbasov Alexander


People also ask

What is method chaining Why is it bad?

The drawback to self-referential method chaining is that you communicate that multiple method calls are required to do something, and that each call builds off the last. If this is not true, then method chaining could be communicating the wrong thing to other programmers.

Which creates a chain class by calling?

The calling of the constructor can be done in two ways: By using this() keyword: It is used when we want to call the current class constructor within the same class. By using super() keyword: It is used when we want to call the superclass constructor from the base class.

Can you chain methods?

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

Which of the following methods are related to method chaining?

method2(). method3(); In the above statement, we have an object (obj) and calling method1() then method2(), after that the method3(). So, calling or invoking methods one after another is known as method chaining.


1 Answers

There is a pretty straightforward pattern called the Builder Pattern where methods basically return a reference to the current object, so that instead of chaining method calls on one another they are chained on the object reference.

The actual Builder pattern described in the Gang of Four book is a little verbose (why create a builder object) instead just return a reference to self from each setXXX() for clean method chaining.

That could look something like this in Python:

class Person: 
   def setName(self, name):
      self.name = name
      return self   ## this is what makes this work

   def setAge(self, age):
      self.age = age;
      return self;

   def setSSN(self, ssn):
      self.ssn = ssn
      return self

And you could create a person like so:

p = Person()
p.setName("Hunter")
 .setAge(24)
 .setSSN("111-22-3333")

Keep in mind that you will actually have to chain the methods with them touching p.a().b().c() because Python doesn't ignore whitespace.

As @MaciejGol notes in the comments you can assign to p like this to chain with whitespace:

p = (
   Person().setName('Hunter')
           .setAge(24)
           .setSSN('111-22-3333')
)

Whether or not this is the best style/idea for Python I can't say, but this is sort of how it would look in Java.

like image 106
Hunter McMillen Avatar answered Oct 15 '22 17:10

Hunter McMillen