0

I'm having tough time understanding def list_entries function . what does _, mean? and inside sorted() there is a regex expr which means replace .md with empty string and then it runs a for loop? it's possible to run a for loop in sorted after regex expression? and lastly if statement checking if it file ends with .md?

I just want to know how this is all put together into sorted()

Thank you for your help.

import re

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage


def list_entries():
    _, filenames = default_storage.listdir("entries")
    return list(sorted(re.sub(r"\.md$", "", filename)for filename in filenames if filename.endswith(".md")))
  • Your question is not focused on single problem (these seem to be multiple questions) so not suitable for SO https://stackoverflow.com/help/on-topic, please take look into https://stackoverflow.com/help/how-to-ask – iklinac Feb 18 '21 at 02:25
  • Look into python list comprehension and https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python. – iklinac Feb 18 '21 at 02:26
  • @Kennith Li : In this situation `_` doesn't have any special meaning, it's just a variable name, the same as any other variable. There is a convention to use `_` where you don't actually need to use a variable, but you have to assign it to something whilst unpacking an iterable. – tim-mccurrach Feb 18 '21 at 02:57
  • This code would probably be much clearer if it was written as `x = ...` followed by `sorted(x)`. You can thinking of everything inside the brackets as an expression that is evaluated and then passed to the function `sorted` – tim-mccurrach Feb 18 '21 at 02:59

0 Answers0