2001 Enchancement Wishlist
Andrew Dalke
dalke at acm.org
Fri Dec 29 16:20:56 EST 2000
Raymond Hettinger wrote:
>1. List.append(elem) and List.pop() are great. Let's add two more
> methods, List.prepend(elem) and List.leftpop(). The goal is to
> make queues,deques, and reverse stacks easier, faster, and clearer
> to implement.
Adding the method does not make things faster. There would need
to be an implementation change so that popping from the left
doesn't take O(N) time. This has come up before. Guido even
mentioned some names for "prepend" and "leftpop" which came from ..
some other language (forgot which).
The thing is, there are reasons for different data structures
like queues over lists. Trying to encapsulate all of them in
one data structure is hard - and trying to do it efficiently is
impossible. Instead, why not develop a library of data
structures for Python, with queue, trees of various sorts,
stacks and (to make some happy :) lists which resize in N*log(N)
time instead of N**2 time as N >> 1000.
BTW, prepend is easily done with an insert at 0.
>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>*'
>>>
> 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
or more specifically:
class MyClass:
pass
_MyClass = MyClass
_MyClass_singleton = None
def MyClass():
global _MyClass_singleton
if _MyClass_singleton is None:
_MyClass_singleton = _MyClass()
return _MyClass_singleton
I'll grant you that it doesn't look as clean, but it isn't
used all that often and the above is a pretty standard way
to write cached/static data. Also, there have been a lot
of threads on special support for singletons in Python.
Oh, and __del__ also does not allow a return value.
>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?
You can get the behaviour you want with a simple wrapper:
(this is untested)
class IndexedDict:
def __init__(self, dict):
self.__dict = dict
def __getitem__(self, key):
try:
return self.__dict[key]
except KeyError:
keys = self.__dict.keys()
try:
return self.__dict[keys[key]]
except IndexError:
raise KeyError, key
def __getattr__(self, name):
return getattr(self.__dict, name)
Several implementation choices are possible (I for one do not
want your behaviour). You can easily implement what you want
from what's already there, so this isn't a needed change to Python.
Andrew
dalke at acm.org
More information about the Python-list
mailing list