Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #222: I'm not sure. Try calling the Internet's head office -- it's in the book.


comp / comp.lang.python / Re: Lists in Python versus other languages

SubjectAuthor
* Lists in Python versus other languagesStefan Ram
`* Re: Lists in Python versus other languagesSebastian Wells
 `* Re: Lists in Python versus other languagesStefan Ram
  `- Re: Lists in Python versus other languagesStefan Ram

1
Subject: Lists in Python versus other languages
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Mon, 24 Jun 2024 10:24 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Lists in Python versus other languages
Date: 24 Jun 2024 10:24:27 GMT
Organization: Stefan Ram
Lines: 27
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <LISP-20240624112258@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 2lF1srZaXF2pDZF+oXjhbAossq0MBwlNgNRGp3t2HY4gZy
Cancel-Lock: sha1:t7t1x/1LC6fbi2Gx4QsMmepaVbw= sha256:MvHXVd5faalnSgZOba5HQEG0eyDoR5YbLWtYzjwxhgk=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Sebastian Wells <sebastian@here.com.invalid> wrote or quoted:
|..etc, taking into account that Python "lists" are really
|arrays, and there's no real Lisp equivalent to tuples,

Well, you could say that, in LISP, the dotted pair

( 1 . ( 2 . NIL ))

represents the list (1 2) while

( 1 . 2 )

represent the tuple "1,2".

|but they're essentially arrays also.

On an implementation level, an array is cache-friendly, while
a linked list is not. LISP lists are linked lists, not arrays.

|Lisp, there's no reader that will give you the original structure
|from its string representation without having to also evaluate it

In Python, the ast module can yield the structure of a module
of Python code (including list and tuple literals) without
the need to execute that code.

Newsgroups: comp.lang.python

Subject: Re: Lists in Python versus other languages
From: Sebastian Wells
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Mon, 24 Jun 2024 10:35 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: sebastian@here.com.invalid (Sebastian Wells)
Newsgroups: comp.lang.python
Subject: Re: Lists in Python versus other languages
Date: Mon, 24 Jun 2024 10:35:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <v5bi4k$7182$5@dont-email.me>
References: <LISP-20240624112258@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 24 Jun 2024 12:35:01 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2787d93bb44b66d3fe4dceb9b5d2d9b9";
logging-data="230658"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/obmX9EQ6ALTEzj61yuVtKv1M5fL3h050="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:PQ13ZfpXxxaPgtrvUtpIMAnjkyw=
View all headers

On 24 Jun 2024 10:24:27 GMT, Stefan Ram wrote:

> Sebastian Wells <sebastian@here.com.invalid> wrote or quoted: |..etc,
> taking into account that Python "lists" are really |arrays, and there's
> no real Lisp equivalent to tuples,
>
> Well, you could say that, in LISP, the dotted pair
>
> ( 1 . ( 2 . NIL ))
>
> represents the list (1 2) while
>
> ( 1 . 2 )
>
> represent the tuple "1,2".
>
> |but they're essentially arrays also.

The thing that makes Python tuples different
from Python lists is that tuples are
immutable. Lisp doesn't have a type that
is "a list (or array) but it's immutable."


> |Lisp, there's no reader that will give you the original structure |from
> its string representation without having to also evaluate it
>
> In Python, the ast module can yield the structure of a module of
> Python code (including list and tuple literals) without the need to
> execute that code.
>

It doesn't yield actual lists or tuples, so you can't use it the
way OP was suggesting, that is, the way you'd use the corresponding
Lisp feature.

Subject: Re: Lists in Python versus other languages
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Mon, 24 Jun 2024 11:25 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Lists in Python versus other languages
Date: 24 Jun 2024 11:25:13 GMT
Organization: Stefan Ram
Lines: 64
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <calls-20240624122249@ram.dialup.fu-berlin.de>
References: <LISP-20240624112258@ram.dialup.fu-berlin.de> <v5bi4k$7182$5@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de kWhC4lGr6mf1aHlEHXJ+3QTtqnrxgW5ikEvwNV3gu0/Cot
Cancel-Lock: sha1:xyQbQo5qAJ4EVm8kgkqPvVKqsXo= sha256:uGEWXJ3ahmiREEpMSO4+Sq0yIbXf/AZtfZme8WutTho=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Sebastian Wells <sebastian@here.com.invalid> wrote or quoted:
>The thing that makes Python tuples different
>from Python lists is that tuples are
>immutable. Lisp doesn't have a type that
>is "a list (or array) but it's immutable."

Yeah, I was thinking about the mathematical term "tuple" here.

In math, one doesn't really have just "tuples," but rather
"2-tuples," "3-tuples," and so on. The number is a fixed part of the
tuple's type. So, if one's dealing with a tuple representation made
of dotted pairs in LISP, one'd need to know how many components are
in it to interpret it correctly. While a list in LISP (with something
like "replace CDR") can be dynamically extended, one can't extend a
tuple because its size is fixed. In that sense, a LISP tuple (in the
sense suggested by me) is more static than a LISP list.

>It doesn't yield actual lists or tuples, so you can't use it the
>way OP was suggesting, that is, the way you'd use the corresponding
>Lisp feature.

If one wants to parse Python code, like a tuple, the ast module
does all the heavy lifting to set up an AST. Turning that into
a real Python tuple takes just a bit of postprocessing.

main.py

import ast as _ast

source = r'''
( 0, 1,( 2, 3 ))
'''[ 1: -1 ]

def postprocess( parse_result ):
if isinstance( parse_result, _ast.Module ):
return postprocess( parse_result.body[ 0 ] )
elif isinstance( parse_result, _ast.Expr ):
return postprocess( parse_result.value )
elif isinstance( parse_result, _ast.Tuple ):
return \
tuple( postprocess( element )for element in parse_result.elts )
elif isinstance( parse_result, _ast.Constant ):
return parse_result.value

print( postprocess( _ast.parse( source )))

stdout

(0, 1, (2, 3))

In the case of simple tuples (with no calls or something),
one can just use "literal_eval".

main.py

import ast

print( ast.literal_eval( '( 0, 1,( 2, 3 ))' ))

stdout

(0, 1, (2, 3))

.

Subject: Re: Lists in Python versus other languages
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Mon, 24 Jun 2024 11:45 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Lists in Python versus other languages
Date: 24 Jun 2024 11:45:11 GMT
Organization: Stefan Ram
Lines: 27
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <match-20240624124240@ram.dialup.fu-berlin.de>
References: <LISP-20240624112258@ram.dialup.fu-berlin.de> <v5bi4k$7182$5@dont-email.me> <calls-20240624122249@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Gpu25x9YPIymjxX55fy7QwY+UDnqf5N7Fu0pqFdnF4aULt
Cancel-Lock: sha1:XR5A1W/3qiPIFFptYDCyqKK+r5A= sha256:O139FUII6dswDTp1BExee8SEb0BRGxHi9iAKEvli9W8=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
>def postprocess( parse_result ):
> if isinstance( parse_result, _ast.Module ):
> return postprocess( parse_result.body[ 0 ] )
> elif isinstance( parse_result, _ast.Expr ):
> return postprocess( parse_result.value )
> elif isinstance( parse_result, _ast.Tuple ):
> return \
> tuple( postprocess( element )for element in parse_result.elts )
> elif isinstance( parse_result, _ast.Constant ):
> return parse_result.value

Or, using "match":

def postprocess( parse_result ):
match parse_result:
case _ast.Module( body=[ entry ]):
return postprocess( entry )
case _ast.Expr( value=value ):
return postprocess( value )
case _ast.Tuple( elts=elements ):
return \
tuple( postprocess( element )for element in elements )
case _ast.Constant( value=value ):
return value

.

1

rocksolid light 0.9.8
clearnet tor