On 26 Jul 2019, at 14:25, Ricky Teachey <ri...@teachey.org> wrote:
>> a = 4
>>
>> with Namespace("ns") as ns:
>> a = 3
>> print(a) # prints "3", not "4"
>>
>> def func():
>> return a # Closes on ns.a, not global a
>>
>> assert isinstance(ns, types.ModuleType)
>> assert ns.name = "ns"
>> assert ns.func() == 3
>> assert a == 4
>
> This is a neat idea and I have wanted something similar myself in situations
> I do not want to instantiate class object or break code out to another module.
>
> Controversial opinion: it may even justify a keyword. But it wouldn't have
> to be a new one, def could work just fine:
>
> def ns:
> a = 3
>
> Food goodly or for badly: something like this would allow one to write code
> similar to how you can in javascript (by simply putting it in curly braces in
> js), without going to the effort of creating a full fledged class:
>
> def a:
> x = 1
> def b:
> x = 2
> def func():
> return x # prints 1
>
This looks pretty nice. Seems like it can be implemented with a subclass of
dict pretty easily if we change the syntax a bit:
a = ns(
x=1,
b=ns(
x=2),
func=lambda self: self.x)
)
?
_______________________________________________
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/KONYW4MWQC42YIREPDHI6HO53PGTUUFH/
Code of Conduct: http://python.org/psf/codeofconduct/