__brace__ (PEP?)

Ron Adam rrr at ronadam.com
Mon May 9 23:19:33 EDT 2005


James Stroud wrote:
> Hello All,
> 
> If "__call__" allows anobject() and "__getitem__" allows anobject[arange], why 
> not have "__brace__" (or some other, better name) for anobject{something}. 
> Such braces might be useful for cross-sectioning nested data structures:
> 
> anary = [[1,2,3],[4,5,6]]
> 
> anary{2}  ==> [3,6]

I could see possibly using it as a user defined container designator, 
but it wouldn't quite work like your examples.

If you used the leading name in the same way as the 'r' in raw strings 
to differentiate a new container type.

class j(User_Container):
     # what ever news/inits needed
     def anary(self, arg):
         # code for anary method

jamesnewcontainer = j{[1,2,3],[4,5,6]}

jamesnewcontainer.anary(2)   ==> [3,6]

 >>>jamesnewcontainer
j{[1,2,3],[4,5,6]}

And this would work as well.

 >>>j{[4,3,2],[1,2,3],[5,4,3]}.anary(1)
[3,2,4]


It doesn't add anything new, but it could make working with custom 
storage objects easier.


> Also, if this exists already, I apologize because I have not seen it in any 
> Python code before and I wouldn't know what to call it for googling.

There might be cases of dictionary use ambiguity.  But I can't think of 
any at the moment.


_Ron





More information about the Python-list mailing list