My secret to high uptime:
while True: try: main() except: pass
that was hilarious xD
Someone is absolutely going to think this is a real recommendation and do it.
Flask developer?
Lurking beginner here, why is this bad?
Basically sweeps errors under rug.
No error handling and go again.
There you go:
# Start an infinite loop because True will always be True while True: # try to run the main function, usually where everything happens try: main() # in the case an exception is raised in the main function simply discard (pass) and restart the loop except: pass
Thank you for that answer! That makes sense.
This gives some better context. https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice
But essentially ignoring every single error a program could generate is not great. It’d be better to know what those errors are and fix/prevent them from occurring in the first place.
deleted by creator
Don’t need to activate your venv to use it.
Just use
venv/bin/python my-file.py
from a script or a terminal from your project root.You don’t need to use venv at all, break the mold and do it all global
/s
Fuck yeah thanks man
Walrus operator - := - envious of the C devs being able to simultaneously assign and return a value? Envy no more, we’ve got it.
For those curious about the drama & lack of wide adoption surrounding the walrus operator
https://dev.to/renegadecoder94/the-controversy-behind-the-walrus-operator-in-python-4k4e
It’s a shame because it’s a really nice feature.
You need to see it to believe it I think. I was generally on the side of “too complex” but then came across instances perfect for it and used it right away and found it pleasant.
I’m still generally on the side of “too complex” though, and think there are probably better things for PSF to work on (cough packaging cough).
I am the Walrus
If using pyenv to support multiple python versions, when creating venvs, make sure to pass
--copies
to it.% python3 -m venv venv --copies
Ordinarily, venv uses symbolic links back to the current version of the python binary. A lot of tools and IDEs don’t traverse symbolic links. That flag actually copies the real binaries over to the venv.
This avoids a metric ton of hard-to-diagnose misery later on.
Yeah, I wish I knew this about a year ago. Thanks.
If you’re on Linux (or Mac), add an alias to your .bashrc:
alias activate="source env/bin/activate"
Now you can activate your venv by just running
activate
in the project root!Very nice, now acti in my rc.
Use fewer loops and more comprehensions.
Also, for the love of $DEITY, embrace EAFP!
EAFP - “Easier to ask for forgiveness than for permission”.
For those who are (like me) unfamiliar with this… acronym?
I forget where I originally found this and Google on my phone was unhelpful.
My favorite annoying trick is
x -=- 1
. It looks like it shouldn’t work because that’s not precisely a valid operator, but it functions as an increment equivalent tox += 1
It works because
-=
still functions are “subtract and assign”, but the second minus applies to the 1 making it -1.You can feign immutablility on class attributes by playing with
__setattr__
. I don’t remember the exact way to do it, but its roughly like this:class YourClass: def __setattr__(self, name, value): if not hasattr(self, name): super().__setattr__(name, value) else: # handle as you wish if the # attr/value already exists. # pass, raise, whatever
I say “feign immutability” because there are still cases in which an attr value can change, such as:
- the underlying attribute contains a list and appending to the list
- the underlying attribute is a class and modifying the class’s attributes
- pretty much anything that does “in place” changes, because so much of python is referential and has side effects.
I have to mention
dataclasses
here, especially withfrozen=True
.Seriously, use dataclasses whenever possible, they’re great.
import antigravity
Uiii, I’m flying!
python -m antigravity
Can I request a hack? How do I handle several different versions of Python installed, which one is used for pip stuff, and how sudo/running as services changes all of this.
You can use pyenv. it will handle everything. https://github.com/pyenv/pyenv
There are like 10,000 different solutions, but I would just recommend using what’s built in to python
If you have multiple versions installed you should be able to call
python3.12
to use 3.12, etcBest practice is to use a different virtual environment for every project, which is basically a copy of an existing installed python version with its own packages folder. Calling pip with the system python installs it for the entire OS. Calling it with sudo puts the packages in a separate package directory reserved for the operating system and can create conflicts and break stuff (as far as I remember, this could have changed in recent versions)
Make a virtual environment with
python3.13 -m venv venv
the 2nd one is the directory name. Instead of calling the system python, call the executable atvenv/bin/python3
If you do
source venv/bin/activate
it will temporarily replace all your bash commands to point to the executables in your venv instead of the system python install (for pip, etc).deactivate
to revert. IDEs should detect the virtual environment in your project folder and automatically activate itI started using hatch lately and really like how I can manage everything from the pyproject.toml file
PyPy exists. (This is news to around 95% of the community.)
If using asyncio is too impenetrable, try using Trio instead. It’s a sensibly-designed asynchronous library, to the point that you’ll find it’s easier to write non-trivial Python programs in Trio from the start, rather than bolting-on async support later.
Asyncio is just plain weird, IMO, exposing more low-level concerns than is customary for Python. Whereas Trio lets you get things done intuitively.
Not necessarily a trick that’s always useful but I always forget this.
You can get async REPL by callingpython -m asyncio
.Also, old trick - in need of simple http server serving static files?
python -m http.server
Using
http.server
is my go-to sanity check method if my configured my network firewall correctly or not.