On Monday 05 April 2004 11:08, Gavin Sinclair wrote:
> And there is an insurmountable limitation (I think):
..
>     chars = words = lines = 0
>     str.each do |line|
>       chars += line.chomp.length
>       words += line.split
>       lines += 1
>     end
..
> I don't believe [Python] can carry outside state into its
> "substitute for blocks".

Feature-by-feature comparisons are useful insofar as they help you
understand differences and similarities between the two languages
being compared.  Consider English and German, for example.  The former
has an "insurmountable" limitation of not having explicit case
markings for the nominative, genitive, accusative, and dative cases
the way German does.

Now, this may be a useful thing to know, but it's useless as far as
comparing the overall expressiveness, naturalness, and ease of use of
these two languages.

As Jim Weirich already pointed out in ruby-talk 96368, closures are a
poor man's objects.  One way of making a function carry state in
Python is to implement it as a callable object:

    $ cat poor_mans_closure.py 
    #!/usr/bin/python

    class Foo:
        "A poor man's closure"

        def __init__(self, state):
            self.__call__ = self._call
            self.state = state

        def _call(self, x, y):
            return x * self.state + y


    foo1 = Foo(1)
    foo2 = Foo(42)

    print foo1(2,3)
    print foo2(2,3)

    $ ./poor_mans_closure.py 
    5
    87