Python-Functional programming

Source: Internet
Author: User

What is functional programming?

Functional programming (functional programming) is a programming paradigm, that is, how to write a program's methodology. At present, we are familiar with imperative programming, where the imperative programming language refers to all the values of the modified variable as the most basic way of computing the language, functional programming language means the output of a program is defined as the input of the mathematical function of the language, Purely functional programming does not have the concept of an internal state, nor does it have side effects.

Features of functional programming

1. Function is Object

Functions can be assigned directly to variables

2. Have attributes and methods

>>> my_sum =sum>>> sum ([a])6>>> My_sum ([a])6>>>dir (my_sum) ['__call__','__class__','__cmp__','__delattr__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__le__','__lt__','__module__','__name__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__self__','__setattr__','__sizeof__','__str__','__subclasshook__']>>> My_sum.__name__'sum'

3, function can do parameters

def F (b): ...      return A +def  F1 (     func,a,b): ... return  >>> F1 (f,1,3)4

The difference between functional programming and imperative programming

Objective: To implement F (a,b,c,d) = (A+B) using functional programming and imperative programming, respectively *c-d

Command-type programming:

def F1 (a,b,c,d):     = A + b    = e * c    = f- d    return  g# command-programming implementation ret = f1 (a,b,c,d)

The function is implemented:

def F_add (A, b    ): return A + bdef  F_sub (b):    return A-def  F _multi (b):    return A * b# functional Programming Implementation RET = F_sub (F_multi (F_add (A , b), c), D)

Python-Functional programming

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.