
Steven D'Aprano wrote:
On Fri, Feb 13, 2015 at 11:19:58AM +0100, Morten Z wrote:
Namespaces are widely used in Python, but it appear that creating a namespace has no short form, like [] for list or {} for dict, but requires the lengthy types.SimpleNamespace, with prior import of types.
from types import SimpleNamespace as NS ns = NS(answer=42)
Python did without a SimpleNamespace type for 20+ years. I don't think it is important enough to deserve dedicated syntax, especially yet another overloading of parentheses. Not everything needs to be syntax.
If there were syntactic support for on-the-fly namespace "packing" and "unpacking" that could have a significant impact on coding style, similar to that of named arguments. Functions often start out returning a tuple and are later modified to return a NamedTuple. For backward compatibility reasons you are either stuck with the original data or you need a workaround like os.stat_result which has more attributes than items. "Named (un)packing" could avoid that.
def f(): ... return a:1, b:2, c:3 f() (a:1, b:2, c:3) :c, :b, :a = f() # order doesn't matter :a, :c = f() # we don't care about b a # by default bound name matches attribute name 1 x:a, y:c = f() # we want a different name x, y (1, 3)