[Python-Dev] constant/enum type in stdlib (original) (raw)
Ron Adam rrr at ronadam.com
Mon Nov 29 04:03:39 CET 2010
- Previous message: [Python-Dev] constant/enum type in stdlib
- Next message: [Python-Dev] constant/enum type in stdlib
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 11/27/2010 04:51 AM, Nick Coghlan wrote:
x = namedvalue("FOO", 1) y = namedvalue("BAR", "Hello World!") z = namedvalue("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))))) setnamedvalues(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
- Previous message: [Python-Dev] constant/enum type in stdlib
- Next message: [Python-Dev] constant/enum type in stdlib
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]