0

So I saw this code somewhere.

1] So why are we returning self at the end of each method?what purpose does it serve?

2] How to interpret the method chaining (last line)- doug.set_legs(4).count_legs().sleep().sleep().sleep()

class pet:
 number_of_legs = 0
 def sleep(self):
 print "zzz "
 return self
 def set_Legs(self, legs):
 self.number_of_legs = legs
 return self
 def count_legs(self):
 print "I have %s legs" % self.number_of_legs
 return self
class dog(pet):
 def bark(self):
 print "Woof"
doug = dog()
doug.set_Legs(4).count_legs().sleep().sleep().sleep() # Any explanation for this chaining & how its operating particularly in this case ??
>>>I have 4 legs
 ZZZ
 ZZZ
 ZZZ
 Out[249]: <__main__.dog instance at 0x7fd5d81917e8> # Why & from where this part getting printed ??
doug.set_legs(4).count_legs().sleep().set_legs(10).count_legs().sleep().sleep()
>>>I have 4 legs
 ZZZ
 I have 10 legs
 ZZZ
 ZZZ
 Out[242]: <__main__.dog instance at 0x7fd5d81917e8> # Why & from where this part getting printed ??
asked Aug 29, 2013 at 7:51
2
  • this is used for method chaining Commented Aug 29, 2013 at 7:55
  • don't take care of the "Out" line that's because you are on ipython ant it give you the return of the last line (try for example 4+4 withou affecting it to a variable ) Commented Aug 29, 2013 at 8:43

4 Answers 4

2

Both questions are related. The methods return self which is the object you are working on and this allows you to chain methods as the result of the first method is the object you call the second method on.

e.g.

doug = dog()
doug.set_Legs(4).count_legs().sleep()

is equivalent to

doug = dog()
doug.set_Legs(4)
doug.count_legs()
doug.sleep()
answered Aug 29, 2013 at 7:56
Sign up to request clarification or add additional context in comments.

2 Comments

BTW what does this mean--- "class Foo(object):" Do I have to write "object" in brackets whenever defining a class, Or is it inheritance of another class called "object". What exactly is this "object" inside the brackets ?
@vinita that is another question that is answered in the python tutorial on the python site
2

This is an example of the Method Chaining pattern. While it's unusual in Python, it can generally be useful if you want to perform multiple actions on the same object. Because of this, it's most often seen in jQuery programming, e.g. for modifying elements on a webpage.

Since the sleep, set_legs and count_legs methods don't return anything of importance, you could just as well return the object the method was called on instead of not returning anything.

By this, if you want to do something else on the same object right after, you can compress it into one line of code.

To explain your line of code:

doug # the Dog object
 .set_Legs(4) # call set_legs on doug, returning doug
 .count_legs() # call count_legs on doug, returning doug
 .sleep() # call sleep on doug, returning doug
 .sleep() # call sleep on doug, returning doug
 .sleep() # call sleep on doug, returning doug

The last printed line is your python interpreter showing the return value of the function, i.e. doug. You could call even more set_legs, count_legs, etc. on that.

The above code is equivalent to doing:

 doug.set_Legs(4)
 doug.count_legs()
 doug.sleep()
 doug.sleep()
 doug.sleep()
answered Aug 29, 2013 at 7:57

4 Comments

It could be worthwhile to note that Method Chaining is - due to whatever reasons - unusual in Python. While in general it could be useful to return self if there is nothing else to return usefully, it is rather useful to return None.
I agree that it's pretty unusual in Python. In the end, it depends on the kind of interface you want to provide and should probably be decided on a case-by-case basis.
But chaining is so much prevalent in NLTK. Eg. as in NLTK with python docs,to read a list of the words in the Brown Corpus,you do this --- nltk.corpus.brown.words()
Unfortunately, that's not method chaining :) nltk.corpus is the name of a python package. brown is an object in that package and it has a words function. (see here)
1

Returning self is useful for method chaining, so you can do

doug.set_Legs(4).count_legs().sleep().sleep().sleep()

instead of

doug.set_Legs(4)
doug.count_legs()
doug.sleep()
doug.sleep()
doug.sleep()

This doesn't look like a very appropriate place to apply it. It can be useful when you want to apply a sequence of transformations to an object, like in jQuery.

As for the method chaining,

doug.set_Legs(4).count_legs().sleep().sleep().sleep()

is equivalent to

temp = doug.set_Legs(4)
temp = temp.count_legs()
temp = temp.sleep()
temp = temp.sleep()
temp = temp.sleep()

which, since all the methods here return self, is equivalent to

doug.set_Legs(4)
doug.count_legs()
doug.sleep()
doug.sleep()
doug.sleep()
answered Aug 29, 2013 at 7:56

Comments

0

1)

def sleep(self):
 print "zzz "
 return self

Your methods can return anything. The only obligation, for a method in a class, is to have 'self' as the first parameter. That's all. You have no reason to return the object. Some of your functions do not need any return statement!

def sleep(self):
 print "zzz "

2) Interpret it in the way it's written: from left to right. Those previous returns were there for you to be able to chain this way (you chain on the return value).

answered Aug 29, 2013 at 8:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.