Re: Lexical scope: converting Perl to Python

2009-06-13 Thread Rhodri James
On Sat, 13 Jun 2009 06:02:53 +0100, Andrew Savige wrote: What is the Pythonic equivalent of Perl's lexical scope, as illustrated by the code snippet above? The useful (to you) pythonic answer depends rather a lot on why you want to do something like that. While you've been given several po

Re: Lexical scope: converting Perl to Python

2009-06-13 Thread John S
On Jun 13, 8:29 am, Nick Craig-Wood wrote: > Andrew Savige wrote: > > >  I'd like to convert the following Perl code to Python: > > >   use strict; > >   { > >     my %private_hash = ( A=>42, B=>69 ); > >     sub public_fn { > >   my $param = shift; > >   return $private_hash{$param}; > >

Re: Lexical scope: converting Perl to Python

2009-06-13 Thread Nick Craig-Wood
Andrew Savige wrote: > > I'd like to convert the following Perl code to Python: > >  use strict; >  { >    my %private_hash = ( A=>42, B=>69 ); >    sub public_fn { > my $param = shift; > return $private_hash{$param}; >    } >  } >  print public_fn("A");    # good:  print

Re: Lexical scope: converting Perl to Python

2009-06-13 Thread I V
On Fri, 12 Jun 2009 22:02:53 -0700, Andrew Savige wrote: > Notice that this code uses Perl's lexical scope to hide the > %private_hash variable, but not the public_fn() function. You might try: def public_fn(param): private_hash = publicfn.private_hash return private_hash[param]

Re: Lexical scope: converting Perl to Python

2009-06-13 Thread jenifer adam
On Jun 13, 10:44 am, Mike Kazantsev wrote: > On Fri, 12 Jun 2009 22:02:53 -0700 (PDT) > > > > > > Andrew Savige wrote: > > I'd like to convert the following Perl code to Python: > > >  use strict; > >  { > >    my %private_hash = ( A=>42, B=>69 ); > >    sub public_fn { > > my $param = shift

Re: Lexical scope: converting Perl to Python

2009-06-12 Thread Mike Kazantsev
On Fri, 12 Jun 2009 22:02:53 -0700 (PDT) Andrew Savige wrote: > I'd like to convert the following Perl code to Python: > >  use strict; >  { >    my %private_hash = ( A=>42, B=>69 ); >    sub public_fn { > my $param = shift; > return $private_hash{$param}; >    } >  } >  print public_f

Re: Lexical scope: converting Perl to Python

2009-06-12 Thread Terry Reedy
Andrew Savige wrote: I'd like to convert the following Perl code to Python: use strict; { my %private_hash = ( A=>42, B=>69 ); sub public_fn { my $param = shift; return $private_hash{$param}; } } print public_fn("A");# good: prints 42 my $x = $private_hash{"A"};

Re: Lexical scope: converting Perl to Python

2009-06-12 Thread Stephen Hansen
> > private_hash = dict( A=42, B=69 ) > def public_fn(param): >return private_hash[param] > print public_fn("A") # good: prints 42 > x = private_hash["A"]# works: oops, hash is in scope > > I'm not happy with that because I'd like to limit the scope of the > private_hash variable s