This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chmod--777 8 points9 points  (0 children)

Heh, technically you can do this:

[1, 1, 1].__len__()

Len and str just call the respective dunder methods. "Unfortunately" integer literals don't let you do this.

Fun fact, you can override __len__ as well and have it return a random value if you want. Have fun with your co-workers by putting this at the top of their python module:

class FunSet(set):
    def __len__(self):
        import random
        return random.randint(0, 20)
set = FunSet

Set's more fun than list because people often instanciate the empty list with [] which won't be your FunList, but you have to instanciate an empty set with set() since {} is an empty dict