Was thinking of all the things that could be done with Antoine's generalization:
The Good
========
d = keyfuncdict(key=str.lower) # case insensitive dict
def track(obj):
logging.info(obj)
return obj
d = keyfuncdict(key=track) # monitored dict
d = keyfuncdict(key=id) # makes benjamin happy
The Bad
=======
d = keyfuncdict(key=tuple) # lets you use lists as keys
d = keyfuncdict(key=repr) # support …
[View More]many kinds of mutable or unhashable keys
d = keyfuncdict(key=pickle.loads) # use anything picklable as a key
d = keyfuncdict(key=getuser) # track one most recent entry per user
The Ugly
========
d = keyfuncdict(key=random.random) # just plain weird
d = keyfuncdict(key=itertools.count().next) # all entries are unique and unretrievable ;-)
def remove(obj):
d.pop(obj)
return obj
d = keyfuncdict(key=remove) # self deleting dict ;-)
Raymond
[View Less]
Hello,
I would like to raise an issue here that I've been discussing at
python-porting.
(And I'd like to preface by saying that I'm not intimately familiar with
Python's innards, so if I make any mistakes please correct me.)
In Python 2.x there was an "unbound method" type. An unbound method would
have an attribute `.im_class` that would refer to the class on which the
method was defined. This allowed users to use the `copy_reg` module to
pickle unbound methods by name. (In a similar way to …
[View More]how functions and
classes are pickled by default.)
In Python 3.x unbound methods are plain functions. There is no way of
knowing on which class they are defined, so therefore it's impossible to
pickle them. It is even impossible to tell `copyreg` to use a custom
reducer:
http://stackoverflow.com/questions/2932742/python-using-copyreg-to-define-r…
(To the people who wonder why would anyone want to pickle unbound methods: I
know that it sounds like a weird thing to do. Keep in mind that sometimes
your objects need to get pickled. For example if you're using the
multiprocessing module, and you pass into it an object that somehow refers
to an unbound method, then that method has to be picklable.)
The idea is: Let's give unbound methods an attribute that will refer to the
class on which they were defined.
What do you think?
Ram.
[View Less]