Keyword/Symbol literals
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 :foo julia> typeof(:foo) Symbol user=> :foo :foo user=> (type :foo) clojure.lang.Keyword user=> `foo user/foo user=> (type `foo) clojure.lang.Symbol *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(...) df.sort(:name) *What do people do now?* Currently people use auto-generated attributes df.sort(df.name) or strings df.sort('name') auto-generated attributes work great until you want to use chained expressions df.change_dataframe().sort(some_new_column_not_in_df) 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 discussion? 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. Best, -Matthew
class Symbol():
def __getattr__(self, var):
t = object()
object.__setattr__(self, var, t)
return t
def __setattr__(self, var, val):
raise AttributeError()
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.
--- Bruce
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
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 :foo julia> typeof(:foo) Symbol
user=> :foo :foo user=> (type :foo) clojure.lang.Keyword
user=> `foo user/foo user=> (type `foo) clojure.lang.Symbol
*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(...) df.sort(:name)
*What do people do now?*
Currently people use auto-generated attributes
df.sort(df.name)
or strings
df.sort('name')
auto-generated attributes work great until you want to use chained expressions
df.change_dataframe().sort(some_new_column_not_in_df)
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 discussion?
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.
Best, -Matthew
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Jan 20, 2015, at 23:48, Stephen Hansen
What do people do now? or strings
df.sort('name')
...
strings work but feel unpleasant
Why?
What does :foo get you that 'foo' doesn't?
One character being saved doesn't seem like a good reason to add syntax.
Mainly they're used to fill in gaps where a language isn't dynamic enough to just use strings, which Python doesn't need. (Sometimes this is claimed to be a strength rather than a weakness. Arguably, using auto-interned symbols instead of strings for things like attribute or global lookup could be more efficient, but I don't think languages like Ruby have shown any demonstrable gain. And we just had a thread from someone who considered it a "bug" that Python allows setattr(spam, '3', 3) when 3 isn't a valid identifier, which symbols "fix" automatically. But even if these were real advantages, Python obviously isn't going to make such a radical change as banning strings for dynamic access.) A language can also add things on top of the symbol/string distinction (besides auto-interning)--e.g., you can add syntactic sugar for dynamic lookup ("foo.:bar" means "getattr(foo, bar)"), or make undefined variables have the value of their symbol so you can LBYL test them, or all kinds of other things--but again, they're all things I don't think Python wants or needs. And without a vague idea for why Python would want to do any of the things that other languages do with their symbol-vs.-string distinctions, this really does seem to come down to just saving one character.
On 2015-01-21, at 10:13 , Andrew Barnert
On Jan 20, 2015, at 23:48, Stephen Hansen
wrote: What do people do now? or strings
df.sort('name')
...
strings work but feel unpleasant
Why?
What does :foo get you that 'foo' doesn't?
One character being saved doesn't seem like a good reason to add syntax.
Mainly they're used to fill in gaps where a language isn't dynamic enough to just use strings, which Python doesn't need.
That's debatable, various languages have used them for fairly different reasons. AFAIK: * Smalltalk had symbols because its strings are mutable, not internable and O(n) comparison (implementation may vary, Squeak's literal strings are/were immutable but I do not know if they were interned). Symbols are always interned and provide O(1) comparison which is useful in many situations * Ruby inherited behaviour and purpose (the community did later add syntactic sugar on top of symbols, like `&:foo` for {|o| o.foo}) * Erlang uses symbols (which it calls atoms) because its strings are linked lists of integers and thus relatively expensive; atoms are literal and interned with identity (across the process too), historically it expected such a small number of atoms they weren't even GC'd * Lisp's use of symbols comes from the metaprogramming side: how do you represent a symbol (a variable name) in a macro? With a string it's not possible to differentiate actual strings and strings-as-identifiers. So symbols it is. And since symbols were in, cheap and useful they could be exposed to the developer. It's got nothing to do with "dynamic enough", that's just nonsense, it has to do with the semantics of the language and its strings, and the string's suitability to some specific purposes or use cases. Which does not mean symbols should be added to Python (let alone that all languages should have symbols).
Yeah, basically this was a fork in the road roughly 25 years ago that Python didn't take. (I was aware of the idea from Lisp, and decided Python could do with just strings instead.) There's no way adding something like this today will suddenly change everything else that is part of having "symbols"; the proposed feature would be lacking the integration with the internals that makes it work well in some other languages. On Wed, Jan 21, 2015 at 1:13 AM, Andrew Barnert < abarnert@yahoo.com.dmarc.invalid> wrote:
On Jan 20, 2015, at 23:48, Stephen Hansen
wrote: *What do people do now?*
or strings
df.sort('name')
...
strings work but feel unpleasant
Why?
What does :foo get you that 'foo' doesn't?
One character being saved doesn't seem like a good reason to add syntax.
Mainly they're used to fill in gaps where a language isn't dynamic enough to just use strings, which Python doesn't need.
(Sometimes this is claimed to be a strength rather than a weakness. Arguably, using auto-interned symbols instead of strings for things like attribute or global lookup could be more efficient, but I don't think languages like Ruby have shown any demonstrable gain. And we just had a thread from someone who considered it a "bug" that Python allows setattr(spam, '3', 3) when 3 isn't a valid identifier, which symbols "fix" automatically. But even if these were real advantages, Python obviously isn't going to make such a radical change as banning strings for dynamic access.)
A language can also add things on top of the symbol/string distinction (besides auto-interning)--e.g., you can add syntactic sugar for dynamic lookup ("foo.:bar" means "getattr(foo, bar)"), or make undefined variables have the value of their symbol so you can LBYL test them, or all kinds of other things--but again, they're all things I don't think Python wants or needs.
And without a vague idea for why Python would want to do any of the things that other languages do with their symbol-vs.-string distinctions, this really does seem to come down to just saving one character.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido)
On Wed, Jan 21, 2015 at 12:42 PM, Guido van Rossum
There's no way adding something like this today will suddenly change everything else that is part of having "symbols"; the proposed feature would be lacking the integration with the internals that makes it work well in some other languages.
Wouldn't it be possible to simply have def Symbol(str): def __new__(cls_, *args): istr = sys.intern(str(*args)) return str.__new__(cls, istr) and have sys.intern() return Symbol instances at some point? I don't see any obvious compatibility problems. How would this be different from adding bool as a subclass of int some time ago? BTW, I think Pandas found [1] the way to have memory-saving string vectors without having a symbol type in the core. [1] http://pandas-docs.github.io/pandas-docs-travis/categorical.html
On Wed, Jan 21, 2015 at 11:11 AM, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
On Wed, Jan 21, 2015 at 12:42 PM, Guido van Rossum
wrote: There's no way adding something like this today will suddenly change everything else that is part of having "symbols"; the proposed feature would be lacking the integration with the internals that makes it work well in some other languages.
Wouldn't it be possible to simply have
def Symbol(str): def __new__(cls_, *args): istr = sys.intern(str(*args)) return str.__new__(cls, istr)
and have sys.intern() return Symbol instances at some point? I don't see any obvious compatibility problems.
I don't see any obvious advantages either.
How would this be different from adding bool as a subclass of int some time ago?
Lack of a use case?
BTW, I think Pandas found [1] the way to have memory-saving string vectors without having a symbol type in the core.
[1] http://pandas-docs.github.io/pandas-docs-travis/categorical.html
Great. -- --Guido van Rossum (python.org/~guido)
On Wed, Jan 21, 2015 at 2:13 PM, Guido van Rossum
Lack of a use case?
My use case comes from interoperability with other languages. I maintain a package which provides an interface [1] between Python and the language [2] called Q. Q has 3 scalar types to represent text data (char, byte and symbol) and the corresponding vector types. When I pass Python strings to Q, I convert them to symbols because that is what users expect most of the time, but as a result, working with char vectors is unnatural. I currently use bytes type for that, but it is really an abuse because bytes should map to byte vectors. I realize that mine is a niche application, but the same problem is likely to arise with other data systems that have "symbol" or categorical type. [1] http://code.kx.com/wiki/Contrib/PyQ [2] http://kx.com/q/d/q.htm
On Wed, Jan 21, 2015 at 11:38 AM, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
On Wed, Jan 21, 2015 at 2:13 PM, Guido van Rossum
wrote: Lack of a use case?
My use case comes from interoperability with other languages. I maintain a package which provides an interface [1] between Python and the language [2] called Q. Q has 3 scalar types to represent text data (char, byte and symbol) and the corresponding vector types. When I pass Python strings to Q, I convert them to symbols because that is what users expect most of the time, but as a result, working with char vectors is unnatural.
couldn't you create a subclass of str that was exactly the same except that it was defined as mapping to a Q symbol? -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On Wed, Jan 21, 2015 at 3:07 PM, Chris Barker
couldn't you create a subclass of str that was exactly the same except that it was defined as mapping to a Q symbol?
I could, but having a dedicated syntax would make things nicer. Here is what I have: ## Pass some python data to q q.symbol = 'abc' q.symbols = ['abc', 'def'] q.char = q('"a"') # no python syntax q.chars = b'abc' q.vector_of_chars = [b'abc', b'def'] q.byte = q('0x01') # no python syntax .. here is what I would rather have q.symbol = `abc q.symbols = [`abc, `def] q.char = q('"a"') # no python syntax q.chars = 'abc' q.vector_of_chars = ['abc', 'def'] q.byte = q('0x01') # no python syntax q.bytes = [b'xyz'] # or bytes([0x78, 0x79, 0x7a]) ..
Hi, On 2015-01-21 14:38, Alexander Belopolsky wrote:
[...] My use case comes from interoperability with other languages. I maintain a package which provides an interface [1] between Python and the language [2] called Q. Q has 3 scalar types to represent text data (char, byte and symbol) and the corresponding vector types. When I pass Python strings to Q, I convert them to symbols because that is what users expect most of the time, but as a result, working with char vectors is unnatural. I currently use bytes type for that, but it is really an abuse because bytes should map to byte vectors.
Hi, after thinking about it some more I've come to believe new, empty
classes are the best way to model symbols in Python (for now at least).
My reasoning is:
- Symbols are tags (i.e. unique names for things).
- In a typed language which has enums or discriminated unions (aka
tagged unions), tags are often created from enums.
- Enums and discriminated unions are isomorphic to base and inheriting
classes.
- Therefore, symbols are classes which could optionally inherit from a
base class.
In your case, you could define a function which dynamically creates
these 'symbols' for you and then stashes them somewhere for re-use.
Below, I've used the calling module's `globals()` dict, but you could
store them away very neatly in some other dict to avoid any possible
name conflicts.
# In called module:
class sym(object): pass
def get_sym(sym_name, d):
if s in d: return d[sym_name]
new_sym = type(sym_name, (sym,), {})
d[sym_name] = new_sym
return new_sym
# In calling module:
def char_to_sym(c):
"Map a 'character' to a symbol."
assert len(c) == 1
return get_sym(c, globals()) # This could be any dict.
def str_to_syms(s): return map(char_to_sym, s)
print str_to_syms("Hello, World!") # ==> [
On Wed, Jan 21, 2015 at 2:11 PM, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
Wouldn't it be possible to simply have
def Symbol(str):
Hmm, I should have tested the code before posting. That should have been a class and the logic would not work without modifying sys.intern first, but it does not kill the idea. Why can't we make interned strings a subclass of strings like bool is a subclass of int?
On Thu, Jan 22, 2015 at 6:18 AM, Alexander Belopolsky
Why can't we make interned strings a subclass of strings like bool is a subclass of int?
My gut feeling is that an interned string is exactly the same thing as a non-interned string, and that it should be possible for sys.intern() to return the original string unchanged. But on the flip side, if you *know* that two strings are interned, you can compare their identities instead of their values; currently, identical interned strings compare equal quickly, but distinct ones still have to be compared character-for-character:
timeit.repeat("x==y","x='a'*65536+'q'; y='a'*65536+'q'") [2.535494999960065, 2.2198410001583397, 2.2210810002870858] timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'q'); y=sys.intern('a'*65536+'q')") [0.056215000338852406, 0.05387900024652481, 0.04834900004789233] timeit.repeat("x==y","x='a'*65536+'x'; y='a'*65536+'y'") [2.53101500030607, 2.2251750002615154, 2.228022000286728] timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'x'); y=sys.intern('a'*65536+'y')") [2.583013000432402, 2.2194420001469553, 2.224679999984801]
If, in the fourth example, the interpreter knew that both those string objects were interned, it could know that they can't possibly be the same string. So is that distinction worth coding into the class hierarchy? ChrisA
On Wed, Jan 21, 2015 at 12:58 PM, Chris Angelico
On Thu, Jan 22, 2015 at 6:18 AM, Alexander Belopolsky
wrote: Why can't we make interned strings a subclass of strings like bool is a subclass of int?
My gut feeling is that an interned string is exactly the same thing as a non-interned string, and that it should be possible for sys.intern() to return the original string unchanged.
I would tend to agree that there is no real difference between an interned and non-interned string in Python. If Python had mutable strings as the base type, that would be different; then having an immutable version of string would be helpful for various reasons.
But on the flip side, if you *know* that two strings are interned, you can compare their identities instead of their values; currently, identical interned strings compare equal quickly, but distinct ones still have to be compared character-for-character:
timeit.repeat("x==y","x='a'*65536+'q'; y='a'*65536+'q'") [2.535494999960065, 2.2198410001583397, 2.2210810002870858] timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'q'); y=sys.intern('a'*65536+'q')") [0.056215000338852406, 0.05387900024652481, 0.04834900004789233] timeit.repeat("x==y","x='a'*65536+'x'; y='a'*65536+'y'") [2.53101500030607, 2.2251750002615154, 2.228022000286728] timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'x'); y=sys.intern('a'*65536+'y')") [2.583013000432402, 2.2194420001469553, 2.224679999984801]
If, in the fourth example, the interpreter knew that both those string objects were interned, it could know that they can't possibly be the same string. So is that distinction worth coding into the class hierarchy?
If such an optimization were desirable, it would be better implemented as a flag that is accessible on the string object, but would only be writable internally (no public API for setting*). This would then allow the string compare to add a check to see if both are interned after checking the pointer values and early exit with a false result. There is not real need (that I know of) to have a class distinction to indicate interning vs a non-mutable string. Chris K *Note that hacks similar to the ones that can be used to change the value of small integers in Python may still function. Depending on internal implementation, it could also be viable to instead have the flag actually perform a lookup into the intern table, but that may negative (much of) the performance benefits.
Chris Angelico wrote:
If, in the fourth example, the interpreter knew that both those string objects were interned, it could know that they can't possibly be the same string. So is that distinction worth coding into the class hierarchy?
I believe there is a flag in the string object indicating that it is interned, so the interpreter *does* know this. It's just not visible to Python code. Switching the class would achieve the same thing and make it visible, but is there any need for it to be? And even if there is, an is_interned() function would achieve the same purpose without having an immutable type magically changing its class. -- Greg
On Thu, Jan 22, 2015 at 9:24 AM, Greg Ewing
Chris Angelico wrote:
If, in the fourth example, the interpreter knew that both those string objects were interned, it could know that they can't possibly be the same string. So is that distinction worth coding into the class hierarchy?
I believe there is a flag in the string object indicating that it is interned, so the interpreter *does* know this. It's just not visible to Python code.
Huh. Okay. It's not made use of for optimizing the == operator, which ought to be easy to do. Or is the cost of checking not worth it? ChrisA
On 22.01.15 00:30, Chris Angelico wrote:
On Thu, Jan 22, 2015 at 9:24 AM, Greg Ewing
wrote: I believe there is a flag in the string object indicating that it is interned, so the interpreter *does* know this. It's just not visible to Python code.
Huh. Okay. It's not made use of for optimizing the == operator, which ought to be easy to do. Or is the cost of checking not worth it?
The cost of checking is not worth it. Comparing hashes (which are already calculated for interned strings) before comparing contents has almost the same effect and this proposition was tested and rejected. This makes double work in dict lookup which is more common case. https://bugs.python.org/issue16286
Alexander Belopolsky wrote:
Why can't we make interned strings a subclass of strings like bool is a subclass of int?
Because a string can change from being non-interned to being interned at some point in its life. I suppose its tp_class could be changed at that point, but that would be surprising for an otherwise immutable type. -- Greg
On Wed, Jan 21, 2015 at 5:18 PM, Greg Ewing
Alexander Belopolsky wrote:
Why can't we make interned strings a subclass of strings like bool is a subclass of int?
Because a string can change from being non-interned to being interned at some point in its life.
I did not know that. Can you show the code that would cause such a change?
Sorry, I misread what you wrote. I thought you suggested that an interned string can get "uninterned" somehow. Yes, sys.intern(x) will change x's status, but as you suggested, it can change x.__class__ as well. However, with a Symbol class in-place, I don't think there will be a need for such transitions. Even now, I don't see sys.intern() in user code too often. On Wed, Jan 21, 2015 at 9:08 PM, Alexander Belopolsky < alexander.belopolsky@gmail.com> wrote:
On Wed, Jan 21, 2015 at 5:18 PM, Greg Ewing
wrote: Alexander Belopolsky wrote:
Why can't we make interned strings a subclass of strings like bool is a subclass of int?
Because a string can change from being non-interned to being interned at some point in its life.
I did not know that. Can you show the code that would cause such a change?
On Jan 21, 2015, at 18:13, Alexander Belopolsky
Sorry, I misread what you wrote. I thought you suggested that an interned string can get "uninterned" somehow. Yes, sys.intern(x) will change x's status, but as you suggested, it can change x.__class__ as well. However, with a Symbol class in-place, I don't think there will be a need for such transitions. Even now, I don't see sys.intern() in user code too often.
Probably because the difference between interned and uninterned strings isn't relevant too often. In Python, there is nothing extra you could do with an interned string or symbol that you can't do with a normal string. So the only conceivable benefit is as a performance optimization. And even that won't come up very often--if your hotspot is string comparisons between static or long-lived strings, most likely you're doing something wrong at a much bigger level (e.g., you've got a long chain of elif comparisons where you should have a dict). It's there on the occasions when it's really needed, but the fact that people rarely use it implies that there isn't a need to expand on it with an interned_string class, or anything else.
On Wed, Jan 21, 2015 at 9:08 PM, Alexander Belopolsky
wrote: On Wed, Jan 21, 2015 at 5:18 PM, Greg Ewing
wrote: Alexander Belopolsky wrote:
Why can't we make interned strings a subclass of strings like bool is a subclass of int?
Because a string can change from being non-interned to being interned at some point in its life.
I did not know that. Can you show the code that would cause such a change?
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Tue, Jan 20, 2015 at 11:48:58PM -0800, Stephen Hansen wrote:
*What do people do now?* or strings
df.sort('name')
...
strings work but feel unpleasant
Why?
What does :foo get you that 'foo' doesn't?
It gets you an atomic data type which isn't a string and isn't an int. The point of data types is what they *don't allow* as much as what they do allow. Symbols is that they are atomic symbolic names, not lists, ints, dicts or strings. 'foo'.upper()[1:] is a meaningful operation. 'foo' is a sequence of characters, so you can slice it, and characters can be uppercased and lowercased and case-folded. Strings have prefixes and suffixes and substrings. If :foo is merely yet another syntax for creating strings, then it is pointless and you should just use a string. But the point of symbols is that they don't support string operations. :foo.upper()[1:] is a meaningless operation, as is :foo + 23. Python already has a number of symbol-like objects in the language: None True and False NotImplemented Ellipsis and possible a few others. With the exception of True and False, most of them have virtually no "interesting" or public methods: e.g. you can convert None into a string, but you can't do None.upper()[1:]. Although, arguably, they are more like Enums than symbols, at least the way Julia uses symbols. http://stackoverflow.com/questions/23480722/what-is-a-symbol-in-julia I'd like to learn more about Julia-style metaprogramming with symbols. The alternative is to treat symbols just as an object with a name and little behaviour (like None), which is not as interesting and more suited to Enums. -- Steve
Steven D'Aprano wrote:
On Tue, Jan 20, 2015 at 11:48:58PM -0800, Stephen Hansen wrote:
What does :foo get you that 'foo' doesn't?
It gets you an atomic data type which isn't a string and isn't an int.
If you want that, you're better off creating your own class. Otherwise, there's nothing to distinguish symbols used for one purpose from symbols used for another, which is the same problem recurring at another level.
Python already has a number of symbol-like objects in the language:
None True and False NotImplemented Ellipsis
And it's not hard to create more, especially now that we have Enums as a provided battery. -- Greg
On Jan 21, 2015 6:21 AM, "Matthew Rocklin"
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 :foo julia> typeof(:foo) Symbol
user=> :foo :foo user=> (type :foo) clojure.lang.Keyword
user=> `foo user/foo user=> (type `foo) clojure.lang.Symbol
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(...) df.sort(:name)
What do people do now?
Currently people use auto-generated attributes
df.sort(df.name)
or strings
df.sort('name')
auto-generated attributes work great until you want to use chained
expressions
df.change_dataframe().sort(some_new_column_not_in_df)
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 discussion?
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.
At best this saves one character, may reduce readability, and would require a huge amount of software to support another code path. Is there any advantage to this besides the one character saved?
On Wed Jan 21 2015 at 07:59:41 Todd
At best this saves one character, may reduce readability, and would require a huge amount of software to support another code path. Is there any advantage to this besides the one character saved?
If symbols are [a subclass of] str, I don't see why a huge amount of software - or indeed any software - would need to do anything special to support it. (Other than the implementation of the feature itself, obviously). That said, I still don't think it's very useful.
On Tue, Jan 20, 2015 at 9:20 PM, Matthew Rocklin
df.sort(df.name)
or strings
df.sort('name')
auto-generated attributes work great until you want to use chained expressions
df.change_dataframe().sort(some_new_column_not_in_df)
I would like to add that Python 3.4 enums would make a better and more idiomatic replacement for auto-generated attributes, e.g. df.columns.name (which may look similar to SQLAlchemy semantics for those familiar). Note how languages such as Ruby, Clojure, and Julia do not have enums and how languages with enums tend to not have symbols. With immutable strings and enums already present, symbols do not add very much value.
Hi,
On 2015-01-21, at 0:20, Matthew Rocklin
[...]
What do people do now?
Currently I use new empty types as symbols. E.g.: class my_symbol: pass x = my_symbol if x == my_symbol: print("yes") Probably there's a nicer way to do it, but the advantage of using types is they are obviously interned. Regards, Yawar
participants (17)
-
Alexander Belopolsky
-
Andrew Barnert
-
Bruce Leban
-
Chris Angelico
-
Chris Barker
-
Chris Kaynor
-
Ed Kellett
-
Greg Ewing
-
Guido van Rossum
-
Masklinn
-
Matthew Rocklin
-
Michael Mitchell
-
Serhiy Storchaka
-
Stephen Hansen
-
Steven D'Aprano
-
Todd
-
Yawar Amin