473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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"]
--
Best regards,
Alexander mailto:al*****************@gmail.com

May 21 '06 #1
5 1282
In article <ma***************************************@python. org>,
Alexander Kozlovsky <al*****************@gmail.com> wrote:
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.
May 21 '06 #2
On Sun, 21 May 2006 16:19:29 +0400,
Alexander Kozlovsky <al*****************@gmail.com> wrote:
How about small syntactic sugar: foo!bar!baz.x = y With this suggestion, mapping!identifier
becomes fully equivalent to mapping["identifier"]


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

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52308

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 21 '06 #3
Roy Smith wrote:
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 Sommers wrote: Take a look at the "Bunch" recipe in the Python Cookbook:

http://aspn.activestate.com/ASPN/Coo...n/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.
--
Best regards,
Alexander mailto:al*****************@gmail.com

May 21 '06 #4
Alexander Kozlovsky wrote:
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.

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 21 '06 #5
Edward Elliott wrote:
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

--
Best regards,
Alexander mailto:al*****************@gmail.com

May 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server...
2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
3
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
1
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different...
6
by: Frank Rachel | last post by:
So I can focus on the correct areas of research, I was wondering if someone could give me the .NET equivelents for this J2EE architecture: JSP's make calls to local JavaBean Controls. The...
3
by: Marty | last post by:
Hi, What is the VB.NET equivalent of the VB6 ADODB.Connector and ADODB.Recordset? Thanks Marty
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
10
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.