How (in)efficient is a list comprehension if you don't assign it?

后端 未结 4 1930
北恋
北恋 2020-12-21 00:08

In this question, I\'m having an argument with a commenter who argues that

for t in threads:
    t.join()

would be better than



        
4条回答
  •  抹茶落季
    2020-12-21 00:56

    You can make a foreach that works as you want. I would recommend against it, as it's not a normal python way of doing things. Untested, but along the lines of this:

    class foreach:
        def __init__(self, object_list):
            self.__object_list = object_list
    
        def __getattr__(self, name):
            def f(*args, **kwargs):
                for obj in self.__object_list:
                    getattr(obj, name)(*args, **kwargs)
            return f
    
    
    foreach(thread_list).join()
    

    It pretty much creates a sort of proxy object that forwards any call with any parameters to all objects.

    (I will most likely have a good chat if you if you would do this in my development team, but it does serve as an example of what is possible in python)

提交回复
热议问题