Python

Python Function Fun Facts

A string identifier is attached to every function at creation time

  • Which is nice for debug:
>>> dance.__name__
'dance'

Functions capture local state

  • Just like JavaScript, (lexical) closure:
  • A closure remembers the values from its enclosing scope when it was created

To check whether something is callable

  • Objects can be made callable, by being assigned function as value
  • So, to check whether an object is callable:
>>> callable(foo)
True/False

Benefit of functions being first class

  • We are able to abstract and pass around behaviours
Standard
Python

Why Use Assertion

And, what is the difference between raising normal errors and assertions? Basically:

Raising ErrorAssertion
Raise ValueError('Input not in range [1, 10]')assert 1 <= v <= 10
Intended for usersIntended for developers
Happens when user action is illegalHappens when code contains bug
To signal to user to take a different course of actionTo easily find where code starts to error
Cannot and should not be disabledCan be disabled during compilation,
therefore should not be used in logics like data validation

Standard