dict!ident as equivalent of dict["ident"]

  • Thread starter Alexander Kozlovsky
  • Start date
A

Alexander Kozlovsky

Hello all!
I have small silly syntax suggestion (SSSS)

In many cases, keys in dictionary-like objects are strings,
and moreover - valid Python identifiers. Something like:

foo["bar"]["baz"].x = y

How about small syntactic sugar:

foo!bar!baz.x = y

With this suggestion, mapping!identifier
becomes fully equivalent to mapping["identifier"]
 
R

Roy Smith

Alexander Kozlovsky said:
Hello all!
I have small silly syntax suggestion (SSSS)

In many cases, keys in dictionary-like objects are strings,
and moreover - valid Python identifiers. Something like:

foo["bar"]["baz"].x = y

How about small syntactic sugar:

foo!bar!baz.x = y

Ugh. Syntactic sugar like that leads to language obesity. Anyway, you can
already do what you want with just a little bit of work. Define a class
(perhaps a subclass of dict, if you like) with a __getattr__ method. Then
you can just do

foo.bar.baz.x = y

with no changes needed to the language.
 
A

Alexander Kozlovsky

Roy said:
Define a class (perhaps a subclass of dict, if you like)
with a __getattr__ method. Then you can just do

foo.bar.baz.x = y

with no changes needed to the language.

I think, your solution is very error-prone. If such enhanced
dictionary contains "keys" key, what is meaning of d.keys?
Is it bound method or dictionary item? After you introduce
such dictionary, you cannot add any new method for it, because
it may destroy user code, if acciddent name clashing take place.

Consider SQLTable class, which have "name" attribute, and
bunch of dynamically created named columns. With __getattr__
method you can use SQLTable.column_1 syntax for column access,
but what if same column have name "name"? It may be very
psychologically inconvenient for user of such class to use
same foo.bar syntax for two different purposes.

In most real cases it is important to distinguish between
two different namespaces - "members" namespace with methods
properties etc., and "items" namespace with truly dynamic
content. In todays Python such distinction accomplished
with two different methods - __getattr(ibute)__ and __getitem__.

It is not possible to successfully use only __getattr__
to serve two different purposes. Hense I suggest use
__getitem__ for its direct purpose, and only add some
syntactic sugar for it

Dan said:
Take a look at the "Bunch" recipe in the Python Cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

This recipe is what is stated in its name - "collector
of a bunch of named stuff". It is not suitable for
elaborate classes with many methods (such as SQLTable),
for above mentioned reason - name cluttering between
methods, properties and dynamic content.
 
E

Edward Elliott

Alexander said:
With this suggestion, mapping!identifier
becomes fully equivalent to mapping["identifier"]

Penny-wise, pound-foolish. Saves 3 character strokes at the cost of a new
special-purpose operator which only works in limited circumstances. To
avoid parsing ambiguity, identifier can only contain (as the name implies)
alphanumerics and _. So your ! is limited not only to dicts but to certain
keys in certain dicts. More complicated than it's worth.
 
A

Alexander Kozlovsky

Edward said:
With this suggestion, mapping!identifier
becomes fully equivalent to mapping["identifier"]

Penny-wise, pound-foolish. Saves 3 character strokes at the cost of a new
special-purpose operator which only works in limited circumstances. To
avoid parsing ambiguity, identifier can only contain (as the name implies)
alphanumerics and _. So your ! is limited not only to dicts but to certain
keys in certain dicts. More complicated than it's worth.

Yes, it is limited use-case, but IMHO important one.
The benefits are:
1. Code looks more neat, because IDE will highlight it as identifier,
and not as string
2. Limited form of code completion (as in PythonWin) becomes possible
3. A bit easier to type, and to read

Anyway, I don't intent strongly on this, I just like to see
common attitude
So your ! is limited not only to dicts

Not only to dict, but to any class with __getitem__ or __setitem__
methods
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top