from operator import add, mul from operator import *
Though you can import everything from
operator, it’s more common to specify. Thus,
from math import pi, sin, cos
Binding two names at once
area, circ = pi * radius * radius, 2 * pi * radius
We can bind the build-in function names to values
>>> max(3, 4) 4 >>> max = 7 >>> max(4, 5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable
How to get “max” back?
__builtins__.max(3, 4) max = __builtins__.max
f is max, g id min, max is min
Types of Expressions
- Within a frame, a name CANNOT be repeated
- Everytime a user-defined function is called, a local frame is created.
Looking up Names in Environments
The current environment is either:
- the global frame alone, or
- a local frame, followed by the global frame
Assignment is a simple means of abstraction, by binding names to values
Function definition is a more powerful means of abstraction, by binding names to expressions
def <name>(<formal parameters>): return <return expression>
Function signature indicates how many arguments a function takes.
Function body defines the computation performed when the function is applied.
Hey! Thanks for reading :P I just want to let you know that if you enjoyed this, you might also like this post on Lecture 3 – Controls.