How to use method chains in Python

Source: Internet
Author: User
method Chain (methods chaining)is a common syntax in an object-oriented programming language that allows developers to make multiple method calls to the same object in the case of referencing the object only once. As an example:

Suppose we have a Foo class that contains two methods--bar and Baz.

We create an instance of the Foo class:

foo = foo ()

If you do not use the method chain, you want to call the bar and Baz method of the object Foo continuously, we have to do this:

Foo.bar () # Call Method Bar () in Object Foo.foo.baz () # Call Method Baz () on object foo.

If we use a method chain, we can do this: Foo.bar (). Baz ()

One benefit of the method chain is that you can reduce the number of times you use object names. The more methods you call, the more times you can reduce them. As a result, this approach can also reduce the amount of code that needs to be read, tested, debugged, and maintained to a certain extent. This is a small benefit, but it is also useful.

Note that one limitation of the method chain is that it can only be used on methods that do not need to return other values, because you need to return the Self object. Even though Python supports returning multiple values with a return statement, this problem may not be resolved.

Here is an example of implementing a method chain in Python:

Class Person:  def name (self, value):    self.name = value    return self  def age (Self, value):    self.age = Value    return self  def introduce:    print ' Hello, my name is ', Self.name, ' and I am ', self.age, ' years old. person = person () person.name ("Earlgrey"). Age (+). Introduce () # = Hello, my name is Earlgrey and I am years old.

The above implementation may be too simple. Let's look at a more realistic method chain usage: Write a string handler string_processor.py, which supports the method chain.

Import Copyclass Stringprocessor (object): ' A class to process strings in various ways.    "Def __init__ (Self, st):" ' Pass a string for st ' "Self._st = st Def lowercase (self):" Make lowercase "     Self._st = Self._st.lower () return self def uppercase (self): "Make uppercase" "Self._st = Self._st.upper () Return self def capitalize (self): "Make first char capital (if letter);     Make other letters lower "Self._st = Self._st.capitalize () return self def delspace (self): ' Delete spaces ' Self._st = Self._st.replace (",") return self def Rep (self): "' Like Python ' s repr ' return Self._st de F dup (self): "Duplicate the Object" "Return Copy.deepcopy (self) def process_string (s): Print SP = Stringprocesso R (s) print ' Original: ', Sp.rep () print ' After uppercase: ', Sp.dup (). Uppercase (). Rep () print ' After lowercase: ', sp.dup ( ). Lowercase (). Rep () print ' after uppercase then capitalize: ', Sp.dup (). uppercase (). \ capitaLize (). Rep () print ' After Delspace: ', Sp.dup (). Delspace (). Rep () def main (): print "Demo of method chaining in Python:" #  Use extra spaces between words to show effect of delspace.  Process_string (' Howz It going? ') Process_string (' The QUIck brOWn fOx ') main ()

Here is the result of the program's operation:

$ python string_processor.pyOriginal:hOWz It   going? After Uppercase:howz IT   going? After Lowercase:howz it   going? After uppercase then capitalize:howz it   going? After delspace:howzitgoing? Original:the   Quick  Brown     foxafter uppercase:the   quick  Brown     foxafter lowercase:the   Quick  Brown     foxafter Uppercase then capitalize:the   quick  Brown     foxafter delspace: Thequickbrownfox

In summary, we can find that the method chain is useful, but overuse may not be good.

How do I use the method chain in Python? I believe we all have a general idea, I hope this article is helpful for everyone to learn.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.