>
> If the core developers don't consider namedtuples important enough to
> get syntactic support, I doubt that namespaces will.
>

This by itself is pretty convincing.

This doesn't "just happen", but because the interpreter does the work


I understand that; my conception of the idea was to add this syntax to the
interpreter, so that it is creating module objects.

One could get around the "why is my function broken?" issue by recycling
another already used keyword, if there could be one made to work (perhaps
import?). But you made a pretty convincing case that this is very unlikely
to happen (though I'd definitely argue for it, I think, if I thought it
were actually a possibility...).

But I ask: how is it possible, then, to create a with block that "gobbles
up" things declared in it and adds those things to some namespace using
existing python syntax...? Won't the syntax HAVE to be changed- or the way
that syntax is currently interpreted be modified- to make any of the
suggested versions of the idea possible?

I currently can't create an object that allows this to work:

with NameSpace('ns') as ns:
    a = 1
assert ns.a=1

That would require blessing a big change by the core devs to work.

One could write a metaclass that does it, I suppose... but it doesn't look
particularly great to me:

import types

class NameSpace(type):
    def __new__(mcls, name, bases, dct):
        if bases:
            raise TypeError("this is a namespace, not a class.")
        mod = types.ModuleType(dct.pop("__qualname__"), dct.pop("__doc__",
None))
        mod.__dict__.update(dct)
        return mod

class ns(metaclass=NameSpace):
    class Desc:
        def __get__(self, obj, cls):
            return None
    d = Desc()

assert ns.d  # descriptor protocol broken as expected

This seems to work. I'm sure there are tons of things wrong with it that
could be fixed. But again, the code isn't very elegant.

>
_______________________________________________
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/TSYOK2JU2PGBCWPQ2GWUHESYVSWJ3HXW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to