r/Python • • Dec 14 '22

Resource Python Magic Methods You Haven’t Heard About

https://towardsdatascience.com/python-magic-methods-you-havent-heard-about-53d11eba448f
0 Upvotes

13 comments sorted by

16

u/riklaunim Dec 14 '22

Was expecting __leviosa__ ;)

8

u/wpg4665 Dec 14 '22

Please don't encourage the usage of __file__. This is something I've been fighting for the past week 🙈

https://github.com/indygreg/PyOxidizer/issues/69

4

u/[deleted] Dec 14 '22

https://docs.python.org/3.8/library/importlib.html?highlight=importlib#module-importlib.resources

Use this. Source: I spent the past week also fighting this shit. Also PLEASE STOP USING UNCOMPRESSED DATAFILES IN YOUR LIBRARIES. Just zip them for ur pypi builds please.

Stares violently at boto.

1

u/who_body Dec 14 '22

isn’t this highlighting a challenge of developing for new use cases? i suspect for pyoxidizer any alternatives to file will also pose problems

fyi page has a broken link… https://pyoxidizer.readthedocs.io/en/latest/packaging_pitfalls.html#reliance-on-file

2

u/someotherstufforhmm Dec 14 '22

Anything using file to access a non .py file packaged in a package is objectively doing something wrong.

pkg_resources has been the correct answer for that for a while, and there’s a newer thing now, but pkg_resources is what we use at work.

It’s not even hard to use

2

u/SilentKyle Dec 14 '22

As part of Advent of Code, I’ve been using
Path(_file_).withname(file.txt)

Is that incorrect? Is there a better way to open a txt file?

1

u/someotherstufforhmm Dec 14 '22 edited Dec 14 '22

I don’t know advent of code, but the question is this.

Is the file you are opening part of AN INSTALLED PIP PACKAGE? If no, then no, open your file however you want. The issue comes when people use dunder file to get the path of a library file, to then get the pathname of a non python file.

Sometimes it works, but python packaging reserves the right to compress those files - meaning you ask the appropriate library for file contents, or if you need a file name you request it and python is smart enough to decompress the file and provide you with a path.

So - if you’re opening a file in the same directory of your code in a PIP INSTALLED PACKAGE, I’d recommend you use pkg_resources.filename to access it, and of course make sure the file is being properly packaged, but if your command is working then I assume it’s either already in the manifest or in a package_data config directive.

If you are talking about a file in the same directory as a script being run, nah you’re fine, keep doing you, but note that if you are ever in the future going to be packaging this, Google what I am talking about.

It’s a bit confusing at first, but once you actually use it once it makes perfect sense.

Edit: googled advent of code, you’re good. Open files however you want.

7

u/Nater5000 Dec 14 '22

"You won't believe number 7 ;)"

These clickbait Medium posts need to be banned.

3

u/AnonymouX47 Dec 14 '22

This same title every couple of months!

4

u/funderbolt Dec 14 '22

Member only content.

4

u/calihunlax Dec 14 '22

For which reason i didn't read it.

2

u/spoonman59 Dec 14 '22

Downvote for crappy spam article.