Instead of braces, Python uses specific indenting instead. It provides the exact same functionality as Perl's scoping braces:
Python:
# function definition def do_thing(name, country): print("Person's name: {}, country: {}\n".format(name, country)) # scoping a file operation with open("file.txt") as fh: # in file scope for line in fh.readlines(): # in for scope print(line) # file handle is now closed automatically
Perl:
# function sub do_thing { my ($name, $country) = @_; print "Person's name: $name, country: $country\n"; } # file { # file handle is scoped to this block open my $fh, '<', 'file.txt' or die $!; while (my $line = <$fh>){ # we're now in a lower-level scope print "$line\n"; } } # file handle is closed and unavailable here
In the latter (Perl) example, I could left-justify all of the code, and it would work the same. Essentially, the indentation is for ease of reading. In Python, the indentation is mandatory. Get one indent spacing incorrect, and your program will either fail, or will cause weird issues, possibly in far-away code.
One of the most basic problems Python newbies have is incorrect indenting. Sometimes Python will warn about it, but not always. However, in Perl, if you miss a closing brace, the process will fail, explaining what went wrong.
"I wondered whether Perl programmers would simply instead specify an array, as named, to a particular scope as specified by parenthesis."
If I'm understanding the question, yes, that's extremely common. It's called a lexical variable. If it's declared within a block, it is not accessible to any other part of the program (unless a reference is returned to it, but I digress).
In reply to Re: Evolution of python
by stevieb
in thread Evolution of python
by betmatt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |