Fwd: Dict access with double-dot (syntactic sugar)

"class attrdict" is a perennial dead-end for intermediate pythonistas who want to save 3 characters/5 keystrokes for item access. Other languages such as javascript allow "somedict.foo" to mean the same as "somedict['foo']", they think, so why not python? Well, there are a number of reasons why not, beginning with the possible conflicts with keywords or any of the magic method names in python. But saving keystrokes is still a reasonable goal. So what about a compromise? Allow "somedict..foo", with two dots, to take that place. It still saves 2 relatively-hard-to-type characters. The "foo" part would of course have to obey attribute/identifier naming rules. So there would be no shortcut for "somedict['$#!%']". But for any identifier-legal foo, the interpreter would just read ..foo as ['foo']. I would not be surprised if I'm not the first person to suggest this. If so, and there's already well-known reasons why this is a bad idea, I apologize. But if the only reason not to is "we never did it that way before" or "it would be too addictive, and so people would never want to use older python versions" or "headache for tools like pylint", I think we should do it.

On Thu, Mar 24, 2011 at 06:23:20AM -0600, Jameson Quinn wrote:
See class DictRecord at http://ppa.cvs.sourceforge.net/viewvc/ppa/qps/qUtils.py d = DictRecord(test="test") print d.test Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Jameson Quinn wrote:
You can have all that in Python as well - you only need to create a dictionary type that maps attribute access to dictionary access. Wrapping existing dictionaries like that is also easily possible. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Mar 24 2011)
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/

On Thu, Mar 24, 2011 at 06:23:20AM -0600, Jameson Quinn wrote:
See class DictRecord at http://ppa.cvs.sourceforge.net/viewvc/ppa/qps/qUtils.py d = DictRecord(test="test") print d.test Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Jameson Quinn wrote:
You can have all that in Python as well - you only need to create a dictionary type that maps attribute access to dictionary access. Wrapping existing dictionaries like that is also easily possible. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Mar 24 2011)
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
participants (4)
-
Jameson Quinn
-
M.-A. Lemburg
-
Oleg Broytman
-
Westley Martínez