It may not be the answer you are looking for, but I would not do it if I were you (and Iβm sure that you cannot easily, anyway).
The reason you shouldn't be because python uses len objects internally on it to perform certain operations. Another reason is pure logic. Your len function, defined above, will return a negative length for empty lists or empty things. This seems very unpleasant to me.
What you can do is override the length method only for certain classes (this can make a lot of sense to you). You can use operator overloading for this, just override the __len__ method in your class:
class MyList(object): def __len__(self,): # Do your thing
You can also view metaclasses, there is a very good stack overflow question on this.
nichochar
source share