2001 Enchancement Wishlist

Raymond Hettinger othello at javanet.com
Sun Dec 31 01:09:45 EST 2000


Andrew Dalke wrote:

> Raymond Hettinger wrote:
> [snip]
> >I recommend that we extend the same courtesy to
> >   the % string formatting operator.  For example:
> >
> >             print "Spam with %s, eggs, and spam" % aUserDictionary
>
> Time machine beat you to it.  I wrote a docstring formatting
> package with templates based on % interpolation using a
> specialized dictionary-like object.  You could do something like:
>
>    "%(module.name|htmlescape|h1)s<p>%(module.docstring|htmlescape)s"
>
> It would parse up the components, get the first term (including
> the "." parts) then apply the results through the set of
> "|" transformations.
>
> >>> class MyDict:
> ...     def __getitem__(self, name):
> ...             print "Asking for", repr(name)
> ...             return "*"
> ...
> >>> d = MyDict()
> >>> "%(module.name|htmlescape|h1)s<p>%(module.docstring|htmlescape)s"%\
> ...     d
> Asking for 'module.name|htmlescape|h1'
> Asking for 'module.docstring|htmlescape'
> '*<p>*'
> >>>
>

Thank you time machine ;) The question remaining is how to hurry along the
existing PEP for extending eval() to accept all mapping objects, as in:

class SpreadSheet:
    _cells = {}
    def __setitem__( self, key, formula ):
        self._cells[key] = formula
    def __getitem__( self, key ):
        return eval( self._cells[key], self )

ss = SpreadSheet()
ss['a1'] = '5'
ss['a2'] = 'a1*5'
print ss['a2']


>
> >   I think [__init__] should continue to return the new object
> > by default but also allow another return item to be substituted.
> > For example:
> >
> >            def __init__( self ):
> >                   if MyClass.aSingleton == None:
> >                         MyClass.aSingleton = self
> >                   return MyClass.aSingleton
>
> Use a factory function for the general case.  For example
> if you need this singleton behaviour you can do:
>
> class MyClass:
>   pass
> _MyClass_singleton = MyClass()
> def MyClass():
>   return _MyClass_singleton
>
>

I was hoping to avoid a factory method or global variable or using
modules to create single instances.  Compared to enriching __init__,
these approaches seem kludgy, break the encapsulation of the class,
and are hard to implement after clients are already making calls to
the constructor.  The decision to make a object a Singleton or to
revoke that decision should be invisible to clients of the class.

Only one of the posts so far reacted positively to the idea, but none
indicated that it would be hard to implement.  It would seem to add
flexibility without breaking anything currently written.

>
> Oh, and __del__ also does not allow a return value.

While I can imagine potentially useful return values for __del__,
that method is unique in that it can be called by a statement rather
than a function.

>
> >4.  Allow dictionaries to respond to sequence operations
> > as if .keys() were in the list object.  Examples:
>
> What if the keys in the dictionary are integers?
>
> data = {1: "one", 3: "three"}
> print data[1]  # This prints "one"
> print data[0]  # Do you want this to be valid or an error?
>

Thank you for ferretting out a design conflict.
Here is a revised wish (borrowed from AWK):

Override the IN keyword for dictionaries and access the keys directly:

knights = { 'lancalot':'good', 'gallahad':'brave', 'robin':'coward' }
for k in knights:
        print k, knights[k]
if 'arthur' in knights:
        print 'Not a king'


Thank you for your insightful comments,

Raymond Hettinger
othello at javanet.com




More information about the Python-list mailing list