in reply to Re^3: Evolution of python
in thread Evolution of python

Not sure what you mean with lexical "scope", but python has closures.

I have problems to see a way to create those without lexical variables.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^5: Evolution of python
by Corion (Patriarch) on Jul 07, 2019 at 19:12 UTC

    Lexical variables and closures don't need each other, unless you define a closure as something that closes over its lexical variables.

    x = "Hello"; # a global variable def make_closure(arg): arg = arg # make arg a lexical variable return lambda y, arg=arg: print(x,y,arg)

    The indentation or braces are (mostly) equivalent in defining where a name/value binding may be visible, but that has nothing to do with closures.

    And the OP issue of not knowing where/how a variable can be declared limited to a scope isn't an issue IMO because both Perl and Python have variables with limited scope which aren't visible "upwards".

      > Lexical variables and closures don't need each other,

      a "closure" over a global variable is commonly known as a function using a global variable ...

      IMHO that's far too trivial to be called a closure. :-p

      Not sure why you are doing this

          arg = arg # make arg a lexical variable

      arg should be a lexical var right away.

      you might want to compare this, a is a lexical and clos a generated closure

      >>> def test(a): ... def clos(): ... return a ... return clos ... >>> x=test(1) >>> y=test(22) >>> x() 1 >>> y() 22

      This is pretty much a demonstration of lexical scope, you will only deal with dynamic scope when using global vars or accessing class attributes.*

      Update

      *) Well if functions call each other and close over the same lexical one might call this a dynamic scope too.

      For instance a recursive closure.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice