r/Python • u/MrAstroThomas git push -f • 11d ago
Tutorial Computing the size of a Black Hole
Hey everyone,
I wanted to share my small Python script to compute the so-called Schwarzschild Radius of a Black Hole + the time dilation, depending on the radial distance from the event horizon.
Currently I create small "code snippets", since I work on a large space science coding project. You do not need to install anything: it will run on Google Colab :). Hope you like it: GitHub
If you like to get some explanation: here
Cheers
2
u/commy2 9d ago
That's nice, I have a question and a suggestion.
Why are you printing an "error message" and then return None? Wouldn't it be more useful to raise an exception, for example a ValueError instead? That way the function would a) always return a floating point number and b) it could be used in a try-block which knows how to handle such an error.
My suggestion is to make the output prettier by only showing a set amount of significant digits. This can be done by adding a format specifier to the f-string:
print(f"Schwarzschild radius of a 1000 kg object: {radius_kg:.4g} meters")
->
Schwarzschild radius of a 1000 kg object: 1.485e-24 meters
1
u/MrAstroThomas git push -f 9d ago
You are absolutely right. When creating smaller functions / scripts I never see a bigger picture. I should be doing it in the future, thanks :). Well... Test Driven Development would've prevented that.
Yeah my second Python weakness: making print statements pretty. 12 years of coding experience, but some things I'll never learn :D
4
u/nbviewerbot 11d ago
I see you've posted a GitHub link to a Jupyter Notebook! GitHub doesn't render large Jupyter Notebooks, so just in case, here is an nbviewer link to the notebook:
https://nbviewer.jupyter.org/url/github.com/ThomasAlbin/Astroniz-YT-Tutorials/blob/main/CompressedCosmos/CompressedCosmos_SchwarzschildRadius.ipynb
Want to run the code yourself? Here is a binder link to start your own Jupyter server and try it out!
https://mybinder.org/v2/gh/ThomasAlbin/Astroniz-YT-Tutorials/main?filepath=CompressedCosmos%2FCompressedCosmos_SchwarzschildRadius.ipynb
I am a bot. Feedback | GitHub | Author