|
|
Subscribe / Log in / New account

Distribution quote of the week

Distribution quote of the week

Posted Oct 25, 2019 15:34 UTC (Fri) by mathstuf (subscriber, #69389)
In reply to: Distribution quote of the week by HelloWorld
Parent article: Distribution quote of the week

> If `os.listdir` were to return a generator, how would you make sure that the underlying file descriptor is closed?

There's `with`, but that is a syntactic thing rather than anything else. Without that, how do you know any `open()` call is closed?

> The elements clearly are arranged in a specific order in the list.

There's a specific order for any given set() instance as well; you just have to access it via iteration rather than the `[]` operator. I was interpreting ordered as in "total ordering" or "weak ordering".


to post comments

Distribution quote of the week

Posted Oct 25, 2019 19:06 UTC (Fri) by gdiscry (subscriber, #91125) [Link]

> > If `os.listdir` were to return a generator, how would you make sure that the underlying file descriptor is closed?
>
> There's `with`, but that is a syntactic thing rather than anything else. Without that, how do you know any `open()` call is closed?

`os.scandir`[1] is the improved `os.listdir` you are looking for 😉.

[1] https://docs.python.org/3.7/library/os.html#os.scandir

Distribution quote of the week

Posted Oct 28, 2019 15:03 UTC (Mon) by HelloWorld (guest, #56129) [Link] (1 responses)

> There's `with`, but that is a syntactic thing rather than anything else. Without that, how do you know any `open()` call is closed?
Sure, you can use `with`, but then it wouldn't be a drop-in replacement for `os.listdir`.

> There's a specific order for any given set() instance as well; you just have to access it via iteration rather than the `[]` operator.

A container that is truly unordered would not give you any way to sequentially iterate over its elements. This sounds completely useless, because how are you supposed to do anything useful with a container you can't iterate over?

The answer is that such containers should offer methods to process the container's elements only in ways where the order doesn't matter. An example would be to calculate the sum of the length of the strings in a container. It's always going to be the same, regardless of the order the set is traversed in, because addition is commutative. Another example would be to apply a pure function to each element of a container and store the results in another unorderd container.

The cats library implements these abstractions for that purpose:

https://github.com/typelevel/cats/blob/master/core/src/ma...
https://github.com/typelevel/cats/blob/master/core/src/ma...

Note the use of CommutativeApplicative and CommutativeMonoid.

Distribution quote of the week

Posted Oct 28, 2019 15:26 UTC (Mon) by mathstuf (subscriber, #69389) [Link]

> Sure, you can use `with`, but then it wouldn't be a drop-in replacement for `os.listdir`.

Without destructors, no replacement could ever ensure it gets closed. Since the only way to use an analogous mechanism in Python is by using `with`, there's nothing to be done.

> <higher-order type safety stuff>

Hmm. Python only recently got even rudimentary optional static type checking support. These kinds of higher-order things being in the core sounds very un-Pythonic. For Coq or Idris, sure. Haskell probably as well, but even hash maps implement Traversable there.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds