Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Evolution of python

by stevieb (Canon)
on Jul 07, 2019 at 15:58 UTC ( [id://11102505]=note: print w/replies, xml ) Need Help??


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
    Scoping rules in python are very different to Perl (and extremely confusing for me*)!

    And to make things worse they changed between Py2 and Py3

    There are nothing like blocks, a function (def or lambda) is the smallest lexical unit.

    A variable on the class level becomes an attribute.

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

    *) Stack overflow has some discussions on that and you'd be surprised how often "experts" contradict themselves.

Re^2: Evolution of python
by betmatt (Scribe) on Jul 07, 2019 at 18:00 UTC
    Instead of braces, Python uses specific indenting instead. It provides the exact same functionality as Perl's scoping braces:

    I don't believe that this is true. I think that my $variable cannot be specified in Python. There does seem to be some differences offered by Perl.

      Python does not have lexical scope for variables. It has more or less dynamic scope.

      This is not relevant to the mechanism that provides the scoping levels, be it braces or indentation.

        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

Re^2: Evolution of python
by thechartist (Monk) on Jul 14, 2019 at 14:06 UTC

    I suspect the semantic white space makes the Python grammar ambiguous from a theoretical standpoint. Anyone have a proof of this?

    I ask as I was trying to think of how tools could be programmed to make the activity of programming easier for people with visual impairments. I think languages with clear block demarcations have advantages here.

      I suspect the semantic white space makes the Python grammar ambiguous from a theoretical standpoint.

      I'd be a bit surprised if that were the case... anyway, there's a Full Grammar specification available, and the source for the grammar is here.

        Lol, I was hoping someone else did a proof. I'm not terribly excited about doing it myself. I had actually looked at those docs, but there wasn't an obvious answer to the question. Thanks though!

        Edit: Old thread, but it appears this question was asked awhile back. Seems to be context-free if I understand this correctly. Is Python context-sensitive?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102505]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-19 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found