# Difference between revisions of "Expression"

[checked revision] | [checked revision] |

Dickson.tsai (Talk | contribs) (Added Basics sidebar) |
(→More complicated example) |
||

Line 50: | Line 50: | ||

# evaluate operator <code>curry(add)(1)</code> | # evaluate operator <code>curry(add)(1)</code> | ||

## evaluate operator <code>curry(add)</code> | ## evaluate operator <code>curry(add)</code> | ||

− | ### evaluate operator <code>curry</code> and find that it is | + | ### evaluate operator <code>curry</code> and find that it is <code>func λ(f)</code> |

### evaluate operand <code>add</code> and find that it is a function | ### evaluate operand <code>add</code> and find that it is a function | ||

### apply <code>func λ(f)</code> to <code>add</code>, returns <code>func λ(x)</code> | ### apply <code>func λ(f)</code> to <code>add</code>, returns <code>func λ(x)</code> |

## Latest revision as of 12:24, 9 July 2014

An**expression**describes a computation and evaluates to a value.

Python Basics |
---|

The Python programming language |

Expression |

Statement |

Function |

Python Data Types |

## Contents

## Primitive expressions

A primitive expression is a single evaluation step: you either look up the value of a name, or take the literal value. For example, variable names, numbers, and strings are primitive expressions.

The following are all primitive expressions: `1`

, `1.2`

, `'test'`

, `True`

, `x`

.

### Variable lookup

A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. In other words, to lookup a name in an environment, start looking in the local frame, then in the parent frame (if it exists), until you get to the global frame. Example:

x = 1 def outer(): def inner(): return x return inner outer()()

to lookup `x`

, look in the local frame `inner`

. Since it is not there, look in the parent frame `outer`

. Since it is not there, look in the parent frame (the global frame) and find `x = 1`

.

## Call expressions

A call expression is an expression that involves a function call. A call expression invokes a function and evaluates to the function's return value. To evaluate a function call:

- evaluate the operator, then from left to right the operands
- apply the function (the value of the operator) to the arguments (the values of the operands)

In general, the operator is the stuff before the set of matching parentheses, and the operands are the stuff inside the set of matching parentheses.

### Simple example

>>> from operator import add, mul >>> add(mul(2, 3), 1)7

Here is the full list of steps:

- evaluate operator of
`add(mul(2, 3), 1)`

and find that it is function`add`

- evaluate operand
`mul(2, 3)`

- evaluate operator
`mul`

and find that it is function`mul`

- evaluate operand
`2`

(primitive) - evaluate operand
`3`

(primitive) - apply function
`mul`

to`2`

and`3`

, returns 6

- evaluate operator
- evaluate operand
`1`

(primitive) - apply function
`add`

to`6`

and`1`

, returns 7

### More complicated example

>>> from operator import add >>> curry = lambda f: lambda x: lambda y: f(x, y) >>> curry(add)(1)(2)3

Here is the full list of steps:

- evaluate operator
`curry(add)(1)`

- evaluate operator
`curry(add)`

- evaluate operator
`curry`

and find that it is`func λ(f)`

- evaluate operand
`add`

and find that it is a function - apply
`func λ(f)`

to`add`

, returns`func λ(x)`

- evaluate operator
- evaluate operand
`1`

(primitive) - apply
`func λ(x)`

to`1`

, returns`func λ(y)`

- evaluate operator
- evaluate operand
`2`

(primitive) - apply
`func λ(y)`

to`2`

, returns 3