views:

832

answers:

5

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 of 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,...) 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.

And please feel free correct this question to proper english and proper IT language.

[edit] I accepted an answer which in only 2 hours totally changed my coding style. I did not know it was that simple. I also appreciate the links you gave, thank you very much.[/edit]

+8  A: 

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.

SilentGhost
this is so cool, thank you, please read also the edit from my question
+1  A: 

See previous questions, for example python-vs-ruby-for-metaprogramming, or general comparisons of programming languages for links to possible explanations.

gimel
+2  A: 

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.

Hank Gay
+2  A: 

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

Van Gale
A: 

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.

Lakshman Prasad