[Python] In Python, when should I use a meta class?

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have gone through this: What is a metaclass in Python?

    But can any one explain more specifically when should I use the meta class concept and when it's very handy?

    Suppose I have a class like below:

    class Book(object):
    CATEGORIES = ['programming','literature','physics']
    def _get_book_name(self,book):
    return book['title']
    def _get_category(self, book):
    for cat in self.CATEGORIES:
    if book['title'].find(cat) > -1:
    return cat
    return "Other"


    if __name__ == '__main__':
    b = Book()
    dummy_book = {'title':'Python Guide of Programming', 'status':'available'}
    print b._get_category(dummy_book)


    For this class.

    In which situation should I use a meta class and why is it useful?

    Thanks in advance.

    Continue reading...

Compartilhe esta Página