Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks of code in Python

Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?

What are the language constructs that exist in Python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does Python lack such constructs?

I have so far understood the lambda thing; it is only one-line, but maybe it comes close. What about "decorators" and yield in this context?

I am also using old Python versions in some projects. Which constructs were introduced in which Python version (2.5, 2.6, etc.) or are planned in future versions?

Can you link interesting articles on the subject that explain this stuff for Python and also comparing to other languages and could be interesting for someone who wants to extend basic Python knowledge?

like image 865
user89021 Avatar asked Apr 20 '09 09:04

user89021


1 Answers

Functions are the first-class members in Python:

def add(x, y):
    return x + y

a = add          # Bind
b = a(34, 1)     # Call

So you can pass functions around all you want. You can do the same with any callable object in Python.

like image 188
SilentGhost Avatar answered Sep 25 '22 11:09

SilentGhost