There is probably some existing python API you can hijack to make custom 
locals() and globals() work everywhere. Perhaps pdb and inspect.stack are good 
places to start; maybe there’s a PDB API to break on every new stack frame and 
maybe you can use inspect to do the proper assignment overrides. 

Python is a very layered language.

Sent from my iPhone

> On May 25, 2019, at 4:53 AM, Yanghao Hua <yanghao...@gmail.com> wrote:
> 
>> On Fri, May 24, 2019 at 10:34 PM Ricky Teachey <ri...@teachey.org> wrote:
>> 
>> You can do things like this with exec():
>> 
>> class SafeDict(dict):
>>    curated_keys = {"a", "b", "c"}
>>    def __setitem__(self, k, v):
>>        if k not i self.curated_keys:
>>            raise Exception(f"{k!r} key not allowed")
>>        super().__setitem__(k, v)
>> 
>> locals_dict = SafeDict()
>> globals_dict = SafeDict()
>> 
>> exec("d=1", locals_dict, globals_dict)  # Exception: 'd' key not allowed
>> 
>> You can do all sorts of things using that technique, including the way 
>> assignment to variables is handled.
> 
> I see your point now. In this case user will need to write HDLs
> probably in below fashion:
> 
> class my_module:
>    def __init__(self, input, output, ...):
>        ...
>    def process(self):
>        exec("signal = 5", locals, globals)
>        # compare this with: signal <== 5
> 
> I fully agree this can work, but also it doesn't seem to be any better
> looking than signal.next = 5. I think there are two important point
> here, first is to make HDL design feels as natural as possible and as
> pythonic as possible as well as leaving very little room for mistakes,
> second is to make HDL design at least as easy and as intuitive as in
> traditional HDLs. And when I see this can actually be achieved easily
> with less than 100 lines of CPython code changes I am tempted to give
> it a try (I am so in love with python ...).
> 
> The other thing I was thinking about is PEP572 assignment expression
> (a := b), if it could be modified to allow user override (e.g. via
> __assign__()), and allow it to be used without bracket like "y :=
> f(x)" if __assign__ is present in y. Then this is even aligned with
> Chisel's assignment operation and I'd be completely happy with it.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QBX47WW5PDA4G77MJ7HPSRMAHIRVXQRW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to