Live data from Hacker News

Use JSON files as if they are Python modules

github.com

1–10 of 67 posts

Re: Use JSON files as if they are Python modules

#4

> Disclaimer: Only do this if you hate yourself and the rest of the world. I'm curious why the author would do this. Just for the fun in it?

Likely for the fun and it might just save time when writing one off scripts or using the interpreter.

I nearly spit my drink out at the name, though.

Re: Use JSON files as if they are Python modules

#6
As a JavaScript developer for the past 5 years or so, when jumping into Python I certainly miss the simple require() which this seems to replicate into Python pretty well!

Having said that I don't think that I would use it for serious things since this isn't really the Python way and I would like my code to be most understood by others. Neat though!

Re: Use JSON files as if they are Python modules

#9

Why do all the comments act like this is bad? Seems fine to me. Explanation would be appreciated :)

You're changing the behavior of import. So if somebody else were to remove the seemingly unused `from jsonsempai import magic` the file would stop working as expected.

Non obvious "magic" code like that tends to be frowned on in general, and especially in the python community

Re: Use JSON files as if they are Python modules

#10

Why do all the comments act like this is bad? Seems fine to me. Explanation would be appreciated :)

Because it monkey patches the way imports work. The context manager isn't bad really, although there are probably more natural ways to create a python object with the same structure as a JSON file without abusing the import system.
Post reply on HN