Re:Show HN: Wat – Deep inspection of Python objects
by igrek51 on Thursday July 25, 2024 @06:27PM
(#41074073)
"import wat" would be great, but Python has some restrictions about modules not being callable. That's why I ended up with longer `from wat import wat`. Not sure but maybe this would be more convenient `import wat; wat.wat / object`
Re:Show HN: Wat – Deep inspection of Python objects
by hprotagonist on Thursday July 25, 2024 @08:04PM
(#41074724)
look at what q does: https://github.com/zestyping/q/blob/cbad74095e20017f33d3ab75...
Re:Show HN: Wat – Deep inspection of Python objects
by Rhapso on Thursday July 25, 2024 @07:10PM
(#41074406)
Python has code execution at import time. Just grab the global context, and overwrite the module with a callable.
Re:Show HN: Wat – Deep inspection of Python objects
by skitter on Thursday July 25, 2024 @08:07PM
(#41074753)
Afaik the import sets the module in the importing module's context only after the code in the imported module is run.
Edit: Oh, you don't mean in the importing module's globals, you mean `sys.modules`. Yeah that works!
Re:Show HN: Wat – Deep inspection of Python objects
by Rhapso on Thursday July 25, 2024 @08:41PM
(#41074936)
actually, no you were right about what I meant but i'll claim credit for helping you realize there was a better way XD
Re:Show HN: Wat – Deep inspection of Python objects
by scrollaway on Thursday July 25, 2024 @07:18PM
(#41074451)
Check out the module “q”! It’s callable, its author talked about how great it would be to have a module level __call__ because the way it was made callable is super wonky.
Re:Show HN: Wat – Deep inspection of Python objects
by enriquto on Friday July 26, 2024 @04:48AM
(#41076936)
> the way it was made callable is super wonky.
why do you say it's wonky? it's just
sys.modules[__name__] = wat
According to the official python documentation [0] of sys.modules:> This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks.
Thus, the modules dictionary is being used as intended, and it has the desired effect. Nothing wonky about that.
Of course, a __call__ method would be better because you could still keep the other functions inside the module. But for a single-function import like "wat" it seems quite natural and sane.
Re:Show HN: Wat – Deep inspection of Python objects
by jkrubin on Thursday July 25, 2024 @10:03PM
(#41075275)
I wrote this a long while ago: https://pypi.org/project/funkify/
Re:Show HN: Wat – Deep inspection of Python objects
by skeledrew on Friday July 26, 2024 @03:13AM
(#41076480)
See snoop[0], which does just this. It also has an install function that registers it in builtins, making it universally available.
Re:Show HN: Wat – Deep inspection of Python objects
by igrek51 on Friday July 26, 2024 @05:34AM
(#41077140)
Thank you all, I learned from you that it's possible to have a callable module. Although `sys.modules[__name__] = wat` looks like a black magic and I'm a bit afraid of locking out to other importable things in this package, I think I'll go for it.
Re:Show HN: Wat – Deep inspection of Python objects
by notpushkin on Friday July 26, 2024 @07:33AM
(#41077751)
It is kinda black magic, and I think it's not something people would expect. You can import other things just fine though:
import sys
def wat(): print("wat")
def bar(): print("bar")
wat.wat = wat
wat.bar = bar
sys.modules[__name__] = wat
... >>> import wat
>>> wat()
wat
>>> from wat import bar
>>> bar()
bar
Edit: I think a better way would be to instead add __call__ to the current module somehow though.
Given the cool nature of this project, I'm surprised they don't offer simply "import wat" with identical usage syntax. Thus inviting curious users to wat/wat in order to discover the trick...