Python __call__ special method practical example

后端 未结 14 1630
感动是毒
感动是毒 2020-11-28 00:28

I know that __call__ method in a class is triggered when the instance of a class is called. However, I have no idea when I can use this special method, because

14条回答
  •  北海茫月
    2020-11-28 01:22

    I find a good place to use callable objects, those that define __call__(), is when using the functional programming capabilities in Python, such as map(), filter(), reduce().

    The best time to use a callable object over a plain function or a lambda function is when the logic is complex and needs to retain some state or uses other info that in not passed to the __call__() function.

    Here's some code that filters file names based upon their filename extension using a callable object and filter().

    Callable:

    import os
    
    class FileAcceptor(object):
        def __init__(self, accepted_extensions):
            self.accepted_extensions = accepted_extensions
    
        def __call__(self, filename):
            base, ext = os.path.splitext(filename)
            return ext in self.accepted_extensions
    
    class ImageFileAcceptor(FileAcceptor):
        def __init__(self):
            image_extensions = ('.jpg', '.jpeg', '.gif', '.bmp')
            super(ImageFileAcceptor, self).__init__(image_extensions)
    

    Usage:

    filenames = [
        'me.jpg',
        'me.txt',
        'friend1.jpg',
        'friend2.bmp',
        'you.jpeg',
        'you.xml']
    
    acceptor = ImageFileAcceptor()
    image_filenames = filter(acceptor, filenames)
    print image_filenames
    

    Output:

    ['me.jpg', 'friend1.jpg', 'friend2.bmp', 'you.jpeg']
    

提交回复
热议问题