<div dir="ltr"><div><font face="monospace, monospace">class Symbol():</font></div><div><font face="monospace, monospace">   </font><span style="font-family:monospace,monospace;white-space:pre"> </span><span style="font-family:monospace,monospace">def __getattr__(self, var):</span></div><div><font face="monospace, monospace"><span style="white-space:pre">        </span>t = object()</font></div><div><font face="monospace, monospace"><span style="white-space:pre">        object.__</span>setattr__(self, var, t)</font></div><div><font face="monospace, monospace"><span style="white-space:pre">        </span>return t</font></div><div><font face="monospace, monospace">    def __setattr__(self, var, val):</font></div><div><font face="monospace, monospace">        raise AttributeError()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">S = Symbol()</font></div><div><br></div><div>Now use <font face="monospace, monospace">S.abc</font> and <font face="monospace, monospace">S.xyz</font> 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.</div><div><br></div><div>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 <font face="monospace, monospace">!foo</font>.</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><font face="arial, helvetica, sans-serif">--- Bruce<br></font><div><div><font face="arial, helvetica, sans-serif">Check out my new puzzle book: <a href="http://Ju.mp/ingToConclusions" target="_blank">http://Ju.mp/ingToConclusions</a></font><br></div></div><div><font face="arial, helvetica, sans-serif">Get it free here: <a href="http://Ju.mp/ingToConclusionsFree" target="_blank">http://Ju.mp/ingToConclusionsFree</a> (available on iOS)</font></div><div><br></div><div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Tue, Jan 20, 2015 at 9:20 PM, Matthew Rocklin <span dir="ltr"><<a href="mailto:mrocklin@gmail.com" target="_blank">mrocklin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It would be nice to have literal keywords/symbols.  By this I mean, terms that look like words but are not previously defined variables.  <div><br></div><div><b>Prior art</b></div><div><b><br></b></div><div>Some other languages prefix by colon, <font face="monospace, monospace">:foo,</font>  or use a backtick,  <font face="monospace, monospace">`foo.</font><div><font face="monospace, monospace"><br></font></div><div><div style="font-family:monospace,monospace">julia> :foo</div><div style="font-family:monospace,monospace">:foo</div><div style="font-family:monospace,monospace">julia> typeof(:foo)<br></div><div style="font-family:monospace,monospace">Symbol</div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace"><div>user=> :foo</div><div>:foo</div><div>user=> (type :foo)</div><div>clojure.lang.Keyword</div></div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace"><div>user=> `foo</div><div>user/foo</div><div>user=> (type `foo)</div><div>clojure.lang.Symbol</div></div><div style="font-family:monospace,monospace"><br></div><div><b><font face="arial, helvetica, sans-serif">Why is this useful?</font></b></div><div style="font-family:monospace,monospace"><b><br></b></div><div><font face="arial, helvetica, sans-serif">One use case in NumPy/Pandas use is to specify columns or fields of data without resorting to strings.  E.g. </font></div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace">df = pandas.load(...)</div><div style="font-family:monospace,monospace">df.sort(:name)</div><div style="font-family:monospace,monospace"><br></div><div><b><font face="arial, helvetica, sans-serif">What do people do now?</font></b></div><div style="font-family:monospace,monospace"><b><br></b></div><div><font face="arial, helvetica, sans-serif">Currently people use auto-generated attributes</font></div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace">df.sort(<a href="http://df.name" target="_blank">df.name</a>) </div><div style="font-family:monospace,monospace"><br></div><div><font face="arial, helvetica, sans-serif">or strings</font></div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace">df.sort('name')</div><div style="font-family:monospace,monospace"><br></div><div><font face="arial, helvetica, sans-serif">auto-generated attributes work great until you want to use chained expressions</font></div><div style="font-family:monospace,monospace"><br></div><div style="font-family:monospace,monospace">df.change_dataframe().sort(some_new_column_not_in_df)</div><div style="font-family:monospace,monospace"><br></div><div><font face="arial, helvetica, sans-serif">strings work but feel unpleasant</font></div></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif"><b>Prior discussion?</b></font></div><div><br></div><div>This is a common language construct so my guess is that it has come up before.  Sadly Google searching the terms <i>keywords</i> and <i>symbols</i> results it a lot of unrelated material.  Can anyone point me to previous discussion?</div><div><br></div><div>There are clearly issues with using <font face="monospace, monospace">:foo</font> in that it overlaps with slice syntax, presumably some other character could be pressed into service if this was found worthwhile.</div></div><div><br></div><div>I can come up with more motivating use cases if desired.</div><div><br></div><div>Best,</div><div>-Matthew</div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div></div>