This may not be the answer you're looking for, but I wouldn't do this if I were you (and I'm pretty sure you can't easily, anyway).
The reason why you shouldn't is that python uses len
internally on it's objects to perform certain operations.
Another reason is pure broken logic. Your len
function defined above would return a negative length for empty lists, or empty things. This seems quite broken to me.
What you can do, is override the length method only on certain classes (this might make a lot of sense for you). For this you can use operator overloading, just override the method __len__
in your class:
class MyList(object):
def __len__(self,):
# Do your thing
You may also want to look into meta classes, there is a very good stack overflow question on this subject.