Python
I have been using Python for a very long time.
Whenever you start a new Python project, always start with a Virtual Environment.
I really need to develop the habit of using virtual environments. Here is the command:
Links
- The Hitchhiker’s Guide to Python is a good book to learn best practices.
- https://realpython.com/ is also really good, has some great resources
Python under the hood
- https://medium.com/swlh/python-memory-management-7181d8a6ae5e
- https://medium.com/@naishasaxena2310/how-python-works-behind-the-scenes-57823eb757bc
Code Style Python Script
Why python is so good
- syntax (no need to declare types upfront)
- Automatic memory management,
- libraries (scipy, numpy, pytorch)
- community support is amazing
Structure Your Project
Check out how this folder is structured.
Testing Your Code
Documentation
Virtual Environments
Random Tips
Use .extend()
instead of .append()
if you want to “append” an array to another array.
Python Design
After taking CS247, I have SUCH a stronger mastery of Object-Oriented Programming. Now, I am actually curious about how does OOP actually work in Python? How do types work in Python?
See https://realpython.com/python-type-checking/
Ahh, so python is ALWAYS dynamically typed. https://stackoverflow.com/questions/4714136/how-to-implement-virtual-methods-in-python. And this is why you don’t even need to declare any function as virtual, since it will NEVER use the static type.
Some new things
In Python, a private member can be defined by using a prefix __ (double underscore). So, in the private modifier’s case, we cannot access the attribute, Learned this at Ericsson
From Andrej Karpathy, so much I am learning from him:
the __call__
method
the zip
function is actually super useful. Just in lecture 2 I am learning about using this function to print out the bigrams:
With the zip, if any of the two inputs is shorter, it just stops, which is why this works.
list(string)
behaves differently than I thought. Say w = "emma"
, then list(w) == ['e', 'm', 'm', 'a']
Thoughts
Python is a really good scripting language, but the more I use Python for OOP, the more I dislike it. There is not strict typing, they do introduce the type thingy like this since like Python 3.8 (I believe?), but that doesn’t provide any optimization.
Python is still fundamentally dynamically typed for everything, which makes it pretty slow and annoying and easy to make mistakes.
With C++, I already know all the types ahead of time, makes it easier to scale. George Hotz talked about this on a Lex Fridman podcast.
Scope Resolution
python
vs. sudo python
Ran into this super weird issue, where I need sudo
permissions, but don’t want packages to install at the sudo
level.
Intended target:
/usr/local/bin/python3.8/dist-packages
Target if you use sudo
/root/.local/bin/python3.8/dist-packages