#🔒 mypy gives me an error which I dont know how to fix

36 messages · Page 1 of 1 (latest)

mint jacinth
#
def make_chapters() -> None:
    """Create all dirs."""
    delete_chapters()
    [os.mkdir(f'Chapter {i}') for i in range(1, 9 + 1)]
"mkdir" does not return a value (it only ever returns None)
sonic harnessBOT
#

@mint jacinth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mint jacinth
#

I might just write

for i in range(1, 9 + 1):
  os.mkdir(f'Chapter {i}') 

But I want this to be done by using 1 line

#

And I still dont get why mypy gives me this error, since I have () -> None:

broken flume
#

U can use map

#

To do it in 1 line

#

!map

sonic harnessBOT
#
Did you mean...

» mutable-default-args
» if-name-main

broken flume
#

!doc map

sonic harnessBOT
#
map

map(function, iterable, *iterables)```
Return an iterator that applies *function* to every item of *iterable*, yielding the results. If additional *iterables* arguments are passed, *function* must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see [`itertools.starmap()`](https://docs.python.org/3/library/itertools.html#itertools.starmap).
broken flume
#

like map(lambda i: os.mkdir(f'Chapter {i}'), range(1, 9 + 1))

#

I’m on mobile so I can’t really check

#

I think that works bc it maps those inputs to the lambda function

#

That technically doesn’t evaluate it tho

#

list(map(lambda i: os.mkdir(f'Chapter {i}'), range(1, 9 + 1)))

#

will make them all evaluate

tight mantle
broken flume
#

map(os.mkdir, [f'Chapter {i}' for i in range(1, 9 + 1)]))

#

Pr sure this works too

mint jacinth
#

Oh no

#

Thats about another line, let me check again

#

Surprisingly linter doesnt give me any error about that, thx for that. Anyway want to try write this by map

tight mantle
broken flume
#

yeah theoretically slightly less useful to use map when you don’t care about the data

#

Because it forces you to return the values vs in a for loop u can just ignore them

#

But in big functions it’s usually more efficient than a for loop if you are trying to generate a huge list from passing values into a function

tight mantle
mint jacinth
#

I'm currently in Beijing, VPN doesn't work correctly, so we have some delays in response, sorry

mint jacinth
#

error: "mkdir" does not return a value (it only ever returns None) [func-returns-value]

#

I think for i in range(1, 9 + 1): os.mkdir(f'Chapter {i}') is the best solution here. Make it simple, isnt it?

#

Thank you for help @broken flume @tight mantle 😎

#

!close

sonic harnessBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.