Python method chain with Huang python sequence article (Methods chaining)

Source: Internet
Author: User

# Python method chain with Huang python sequence article (Methods chaining)


# # Write this article for the reason, a friend said the following code can not understand.

Choice = Raw_input ("Please input:\n"). Strip () [0].lower ()

Many of these are not things for experienced programmers,

But for beginners, it's a bit too big to see such a grammatical head.


# # This is actually the concept of the method chain in object-oriented.

# # See the definition of method chaining on Wikipedia


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 is chained together in a single statement without requiring

Variables to store the intermediate results.

Local variable declarations is syntactic

Sugar because of the difficulty humans has with deeply nested method calls.

A method chain is also known as a train wreck due to the increase

In the number of methods this come one after another in the same

Line, occurs as more methods is chained together

Even though line breaks is often added between methods.


# # Specifically in Python, see Huanggo's analysis:


Some Python beginners make successive calls to the Python method not very clear, like mirrors.

Python everything is an object, the object calls its method, if the return value is returned, the value is also the object,

This return value also has a method, of course, you can call its method with the dot number,

In this way, the Python method chain is called.


# # How to design a method chain Python code



# Coding:utf-8

"""

How to learn to program by learning Python

Https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md

Huanggo Python Remote Video training course

Https://github.com/pythonpeixun/article/blob/master/index.md

Huanggo Python training preview video playback address

Https://github.com/pythonpeixun/article/blob/master/python_shiping.md

Huanggo Python Training Consulting qq:1465376564

"""



class Person (object):

"" "Method Chain Small Sample" ""


def name (self, value):

Self.name = value

Return self # Returns the instance object itself before invoking the instance object's method.


def work (self, value):

self.working = value

return self


def introduce (self):

Print "Hello, my Name:", Self.name, ", My work:", self.working, ", teach beginners to learn to program!"


person = person ()

Person.name ("Brother Huang"). Work ("Huanggo python Training"). Introduce ()



# # PHP Method chain code


<?php

/*

Huanggo PHP Training Consulting qq:1465376564

Https://github.com/pythonpeixun/article/blob/master/php_education.md

*/



Class person{

Public $name;

Public $working;


Public Function SetName ($value) {

$this->name = $value;

return $this;

}


Public function work ($value) {

$this->working = $value;

return $this;

}


Public function introduce () {

echo "Hello, my Name:". $this->name. ", My work:". $this->working. ", teach beginners to learn programming!\n";

}

}


$person = new Person ();

$person->setname ("Huang elder brother")->work ("Huanggo PHP Training")->introduce ();



[Click Huanggo python Training preview video playback address] (HTTPS://GITHUB.COM/PYTHONPEIXUN/ARTICLE/BLOB/MASTER/PYTHON_SHIPING.MD)


[Huanggo Python remote video training course] (HTTPS://GITHUB.COM/PYTHONPEIXUN/ARTICLE/BLOB/MASTER/INDEX.MD)


This article is from the "Python Enthusiasts" blog, so be sure to keep this source http://pythonpeixun.blog.51cto.com/7195558/1765241

Python method chain with Huang python sequence article (Methods chaining)

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.