in reply to Evolution of python
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Evolution of python
by LanX (Saint) on Jul 07, 2019 at 18:26 UTC | |
|
Re^2: Evolution of python
by betmatt (Scribe) on Jul 07, 2019 at 18:00 UTC | |
by Corion (Patriarch) on Jul 07, 2019 at 18:05 UTC | |
by LanX (Saint) on Jul 07, 2019 at 18:31 UTC | |
by Corion (Patriarch) on Jul 07, 2019 at 19:12 UTC | |
by LanX (Saint) on Jul 07, 2019 at 19:52 UTC | |
|
Re^2: Evolution of python
by thechartist (Monk) on Jul 14, 2019 at 14:06 UTC | |
by haukex (Archbishop) on Jul 14, 2019 at 14:14 UTC | |
by thechartist (Monk) on Jul 14, 2019 at 14:41 UTC |