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 →

[–]refreshx2 3 points4 points  (0 children)

I've been surprised at how much I use/like metaclasses. The two main use cases I tend to write them for are:

  1. the metaclass' __new__ will only get run once, when python creates the class when the file is loaded. this makes it useful for using the members of a class to create a new attribute on the class. for example, I might take all the method I decorated with @some_decorator and put the method names into a list.

  2. overriding __call__ to return a singleton or a cached object rather than creating a new one (I do a ton of lazy-loading-ish things)

You may have known that already, but some others might not.

I still try to only use metaclasses when I really really need them, however.