1 |
On 11/10/2014 12:32 PM, Alexander Berntsen wrote: |
2 |
> On 10/11/14 19:21, Zac Medico wrote: |
3 |
>> Well, you'll have to clarify what's so "terrible" about it. Note that I didn't |
4 |
>> use os.path.isdir or similar because those functions hide all kinds of relevant |
5 |
>> exceptions, such as EACCES. |
6 |
> I wish there were a more declarative way of expressing this in Python. |
7 |
> It's extremely imperative. |
8 |
|
9 |
We could certainly express it in a way that doesn't involve any mutating |
10 |
loop control variables, but ultimately that's going to lead to more |
11 |
lines of code, and it will leave imperative programmers wondering why we |
12 |
didn't choose a simpler and more succinct approach. |
13 |
|
14 |
For example, we could create an class for iterating over the paths from |
15 |
a given path down to the root directory. Then we could create a function |
16 |
which selects the first element from that iterator that exists. Once the |
17 |
class and function are implemented, their usage would be very succinct: |
18 |
|
19 |
first_parent = first_existing(iter_parents(path)) |
20 |
-- |
21 |
Thanks, |
22 |
Zac |