My name is James and I work as a software engineer in the UK. I do coding videos, released every Friday. ----------------- Looking to learn Python?
- I run a Udemy course with 16+ hours of video, 32+ coding assignments, 20 quizzes, and 5 projects which can be included in your first portfolio. This is updated monthly with a new job interview style question!
- I also run a functional Python course which focuses on using List, Set, Dict and generator comprehensions. It's full of exercises, quizzes and is updated monthly with a challenging question.
Neat! I suppose you could calculate a prefix-sum by accumulating with addition? If so, that's very handy because I am getting tired of doing it manually every time
Hi James, hope you are doing fine and nice video! I followed the steps of creating the virtual environment (in my case I used the command "python -m venv <envname>"), installing poetry, creating the project (poetry new <name>) and adding the dependencies to the .toml file but I'm having a little issue. When I create the main.py file and try to import fastapi, apparently it is not install within the virtual env. Should I do something else? I'm running it on Windows, I suppose there should not be a problem right? Thanks and look forward to hearing from you.
Hello, you’re right yes, it can be seen as a while loop with an automatic breaking mechanism. You could achieve this with a normal loop but I quite enjoy these neat one liner functions :)
It depends on how the lambda is used. In a list comprehension everything is computed at once and stored in memory. If we use map/lambda then we get an iterator, so it means that items are only computed when they are 'asked' for, such as using the next() function on the iterator.
I was one of the people who voted "Don't know" on the poll, and now watching this, I genuinely think I'd vote "No" if i were to vote again. I consider myself a perfectionist, but I honestly can't say I've experienced those symptoms. Sounds like an outlier in that 😅
It's quite similar! But doing it as a list comprehension means the everything is computed at once. Using filter allows us to only compute the items as needed, and we can use the next() function to retrieve the filtered items one by one.
Hi! Hope you are doing well! I understand you can use context manager not only for handling files, but for example, connecting to a database and automatically close the connection when you are done. I have also seen some cases where you instantiate a class with context managers, what is the purpose here? Thank you.
If you wanted to use a context manager with a custom class.. I would suggest that this is done with classes that specifically operate on files, database connections etc.. This is not super common however, as we do generally have tools that can operate on files/database connections for us :) I wanted to show the example class in the video to give a deeper understanding as to how the __enter__ & __exit__ methods are called :)
One good use for this is API clients, especially asynchronous ones which need to close any async sessions and event loops and such. Otherwise it can just a nice syntax honestly.
Hello :) So I chose to keep end_time a local variable to the __exit__ method because I’m only using it in the scope of that method. although, there would be no harm in making it an instance attribute!
Wow that xor solution is amazing! I always forget these unary operations exist and probably would've gravitated towards either the first or third solutions myself. Nice one!
Nice video my guy! Now having seen how Tox works, looks like the main difference between it and Nox is that the latter is all set up in Python. Much of the rest of the functionality looks to be very similar though.
@@pythonwithjames I just saw your video about Ruff, and a had a look at the documentation, it’s amazing how useful the library is. Usually in my projects, I use black and isort, I’ll start using ruff from now on.
So if it didn't have the decorator it would be a regular method. So it would be called like: instance.method() Which might be acceptable for some use cases, but I think it's generally better to reserve methods for 'actions' a class can do (think of the list.append() & list.extend() methods). A property acts more like an attribute. Hope I've cleared it up :)