10

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?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
user89021
  • 14,784
  • 16
  • 53
  • 65

4 Answers4

10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
3

lambda is the closest equivalent to a Ruby block, and the restriction to one line is intentional. It is typically argued that multiline anonymous functions (blocks, in Ruby) are usually less readable than defining the function somewhere with a name and passing that, as illustrated in SilentGhost's answer.

Community
  • 1
  • 1
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
3

There are good discussions on comp.lang.python that compare to other languages:

Van Gale
  • 43,536
  • 9
  • 71
  • 81
0

The def is equivalent of an assignment statement which only binds the function object to the object reference variable.

The object reference variable can then be used to call the function object to execute.

lprsd
  • 84,407
  • 47
  • 135
  • 168