[Python-ideas] Keyword/Symbol literals
bruce at leban.us
Wed Jan 21 08:36:32 CET 2015
def __getattr__(self, var):
t = object()
object.__setattr__(self, var, t)
def __setattr__(self, var, val):
S = Symbol()
Now use S.abc and S.xyz etc. They automatically get created as unique
objects. Trying to set one of the values raises. With a little more work
you can make it pretty printable.
Is this a valuable enough feature to build into the language and dedicate a
special character to it? I think you'd have to have a lot of use cases and
a lot of usage. If it were added I think the best character to use without
colliding with anything would be !foo.
Check out my new puzzle book: http://Ju.mp/ingToConclusions
Get it free here: http://Ju.mp/ingToConclusionsFree (available on iOS)
On Tue, Jan 20, 2015 at 9:20 PM, Matthew Rocklin <mrocklin at gmail.com> wrote:
> It would be nice to have literal keywords/symbols. By this I mean, terms
> that look like words but are not previously defined variables.
> *Prior art*
> Some other languages prefix by colon, :foo, or use a backtick, `foo.
> julia> :foo
> julia> typeof(:foo)
> user=> :foo
> user=> (type :foo)
> user=> `foo
> user=> (type `foo)
> *Why is this useful?*
> One use case in NumPy/Pandas use is to specify columns or fields of data
> without resorting to strings. E.g.
> df = pandas.load(...)
> *What do people do now?*
> Currently people use auto-generated attributes
> or strings
> auto-generated attributes work great until you want to use chained
> strings work but feel unpleasant
> *Prior discussion?*
> This is a common language construct so my guess is that it has come up
> before. Sadly Google searching the terms *keywords* and *symbols*
> results it a lot of unrelated material. Can anyone point me to previous
> There are clearly issues with using :foo in that it overlaps with slice
> syntax, presumably some other character could be pressed into service if
> this was found worthwhile.
> I can come up with more motivating use cases if desired.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas