[Python-Dev] constant/enum type in stdlib
Ron Adam
rrr at ronadam.com
Mon Nov 29 04:03:39 CET 2010
On 11/27/2010 04:51 AM, Nick Coghlan wrote:
> x = named_value("FOO", 1)
> y = named_value("BAR", "Hello World!")
> z = named_value("BAZ", dict(a=1, b=2, c=3))
>
> print(x, y, z, sep="\n")
> print("\n".join(map(repr, (x, y, z))))
> print("\n".join(map(str, map(type, (x, y, z)))))
>
> set_named_values(globals(), foo=x._raw(), bar=y._raw(), baz=z._raw())
> print("\n".join(map(repr, (foo, bar, baz))))
> print(type(x) is type(foo), type(y) is type(bar), type(z) is type(baz))
>
> ==========================================================================
>
> # Session output for the last 6 lines
>>>> >>> print(x, y, z, sep="\n")
> 1
> Hello World!
> {'a': 1, 'c': 3, 'b': 2}
>
>>>> >>> print("\n".join(map(repr, (x, y, z))))
> FOO=1
> BAR='Hello World!'
> BAZ={'a': 1, 'c': 3, 'b': 2}
This reminds me of python annotations. Which seem like an already
forgotten new feature. Maybe they can help with this?
It does associate additional info to names and creates a nice dictionary to
reference.
>>> def name_values( FOO: 1,
BAR: "Hello World!",
BAZ: dict(a=1, b=2, c=3) ):
... return FOO, BAR, BAZ
...
>>> foo(1,2,3)
(1, 2, 3)
>>> foo.__annotations__
{'BAR': 'Hello World!', 'FOO': 1, 'BAZ': {'a': 1, 'c': 3, 'b': 2}}
Cheers,
Ron
More information about the Python-Dev
mailing list