Sujet : Re: Python (was Re: I did not inhale)
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.unix.shell comp.unix.programmer comp.lang.miscDate : 23. Aug 2024, 09:19:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <va9d5a$qobt$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 23/08/2024 02:19, Lawrence D'Oliveiro wrote:
On Thu, 22 Aug 2024 12:47:16 +0200, David Brown wrote:
On 22/08/2024 11:02, Lawrence D'Oliveiro wrote:
>
Python prohibits those space/tab inconsistencies.
>
No, it does not. Python treats tabs (at the start of lines, which is
the only relevant point here) as 8 spaces by default. You can change
that with command-line flags if you want. But it is quite happy with
mixtures of tabs and spaces as long as the result after tab-to-space
conversion is consistent with Python syntax.
src = \
"""
def fun() :
\t\tprint("line 1")
\t\x20\x20\x20\x20\x20\x20\x20\x20print("line 2")
fun()
"""
exec(src)
Output:
TabError: inconsistent use of tabs and spaces in indentation
src = \
"""
def fun() :
\t\x20\x20\x20\x20\x20\x20\x20\x20print("line 1")
\t\x20\x20\x20\x20\x20\x20\x20\x20print("line 2")
fun()
"""
Mixtures of tabs and spaces are accepted without complaint.
But you are right that some of the worst or most dangerous cases are rejected by Python. Python 3 is stricter here than Python 2 was, and much of my long-term code is Python 2.
(The incompatibilities between Python 2 and Python 3 are another pain in Python. Equally, however, it avoids the pain seen in C and C++ where backwards compatibility can limit new features and force poor features to remain valid.)