3

I'm attempting to make it so that, instead of raising a NameError when an undefined variable is referenced, it instead calls a function that I've defined to get the value.

Background

I'm a relatively advanced Python programmer, and no beginner. This question is not for practical application but for personal learning.

What I've tried so far

I tried to use __builtins__.__missing__ as my function, but it only worked in shell. This works in the Python interactive console on 3.9.1:

>>> __builtins__=type('', (dict,), {'__missing__': lambda *args: args[1]})(__builtins__.__dict__)
>>> print(variable)
variable

However, when I run it as a program, it throws NameError: name 'variable' is not defined

__builtins__=type('', (dict,), {'__missing__': lambda *args: args[1]})(__builtins__.__dict__)
print(variable)

Question

What's going on here, and how can I handle this?

Mous
  • 953
  • 3
  • 14
  • This looks relevant: https://stackoverflow.com/questions/11181519/whats-the-difference-between-builtin-and-builtins – Barmar Mar 01 '22 at 23:48
  • Thanks. However, changing the first `__builtins__` to `__builtin__` did not solve my issue. – Mous Mar 01 '22 at 23:50
  • The fact that this works in the repl is probably a weird quirk (I suspect the eval part of the loop just takes keeps replacing `__builtins__` in the dynamic execution). You shouldn't expect this to work in Python generally. – juanpa.arrivillaga Mar 02 '22 at 01:20
  • So, the important thing to understand is you *can* modify `__builtins__` by actually modifying the object, but *assignment of some other mapping to that name will not work because that doesn't actually mutate the built-ins namespace* – juanpa.arrivillaga Mar 02 '22 at 01:22
  • Thanks, @juanpa.arrivillaga. What can I do to give `__builtins__` a `__missing__` method then? – Mous Mar 02 '22 at 03:47
  • Also, @Barmar, I don't believe the `__builtin__` module exists in Python 3. It's been replaced with `builtins`, but changing this doesn't even work in shell. – Mous Mar 02 '22 at 05:28
  • 2
    This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to accomplish by modifying `__builtins__`? – Barmar Mar 02 '22 at 15:39
  • As near as I can tell, this is not an IDLE issue. On Windows with 3.11.0a5, I get the same behavior when running in the regular interactive console or as a file. – Terry Jan Reedy Mar 02 '22 at 18:32
  • @Barmar thanks for your response. What I'm aiming to do is to make it so that, instead of raising a `NameError` when an undefined variable is referenced, it instead calls a function that I've defined to get the value. @TerryJanReedy confirmed with 3.9.1 in shell and in file. – Mous Mar 02 '22 at 20:24
  • You should say that in the question, and ask how to customize the handling of undefined variable references. – Barmar Mar 02 '22 at 20:27
  • Should I edit the question or pose a new question? – Mous Mar 02 '22 at 20:28
  • I think you could edit this question. Say what you're trying to do, and then show this as the code you tried to use, but it didn't work. – Barmar Mar 02 '22 at 20:29
  • IMO, you should abandon the idea. If you need a name to resolve to particular value, define the name. – chepner Mar 02 '22 at 20:29
  • @chepner I'm no beginner to Python, and this question is for personal learning rather than practical application. – Mous Mar 02 '22 at 20:58
  • I've found a solution involving overriding the `globals()` dictionary by using `ctypes`, but it's extremely hacky. Does a pythonic solution exist? – Mous May 15 '22 at 23:26

0 Answers0