#🔒 mypy gives me an error which I dont know how to fix
36 messages · Page 1 of 1 (latest)
@mint jacinth
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.
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:
» mutable-default-args
» if-name-main
!doc 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).
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
you can just write it as 1 line then:
for i in range(1, 9 + 1): os.mkdir(f'Chapter {i}')```
the line break is optional, but every statement within the block has to be on that line, and it cannot have blocks within
flake8 E902 :)
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
map is a lazy iterable, so for it to do anything you have to wrap it in a call to something like list, as done here
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
suppose you tried a list comprehension again, but placed the mkdir calls in the condition:
[0 for i in range(1, 9+1) if os.mkdir(f'Chapter {i}')]```
would it give you another IDE error?
I'm currently in Beijing, VPN doesn't work correctly, so we have some delays in response, sorry
I got the same mypy error
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
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.