Cute python: Functional programming in Python, part 3rd

Source: Internet
Author: User

An expression binding

A reader who will never be satisfied with a partial solution-Richard Davies-raises the question of whether we can always move the binding to a separate expression. Let's take a quick look at why we want to do this and show a great way for Comp.lang.python contributors.

Let's first review the bindings class in the functional module. By using the properties of that class, we can ensure that a special name has only one meaning within the given block's scope:

Listing 1: The Python FP section with protected rebind

>>>

     from

     functional

     import

     *
>>> let = Bindings()
>>> let.car =

     lambda

     lst: lst[0]
>>> let.car =

     lambda

     lst: lst[2]
Traceback (innermost last):
File "<stdin>", line 1,

     in

     ?
File "d:\tools\functional.py", line 976,

     in

     __setattr__

     raise

     BindingError, "Binding '%s' cannot be modified." % name
functional.BindingError: Binding 'car' cannot be modified.
>>> let.car(range(10))
0

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.