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
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};
> >
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
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]
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
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
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"};
>
> 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