Python: Iterating through constructor's arguments

前端 未结 9 1860
囚心锁ツ
囚心锁ツ 2021-02-05 15:07

I often find myself writing class constructors like this:

class foo:
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        self.arg2 = arg2
         


        
9条回答
  •  深忆病人
    2021-02-05 15:55

    As others have noted, you should probably stick to your original 'pythonic' method in most cases.

    However, if you really want to go the whole nine yards, here's some code that neatly deals with args, keyword args if desired, and avoids boilerplate repetition:

    def convert_all_args_to_attribs(self, class_locals):
        class_locals.pop('self')
        if 'kwargs' in class_locals:
            vars(self).update(class_locals.pop('kwargs'))
        vars(self).update(class_locals)
    
    class FooCls:
        def __init__(self, foo, bar):
            convert_all_args_to_attribs(self, locals())
    
    class FooClsWithKeywords:
        def __init__(self, foo, bar, **kwargs):
            convert_all_args_to_attribs(self, locals())
    
    f1 = FooCls(1,2)
    f2 = FooClsWithKeywords(3,4, cheese='stilton')
    
    
    print vars(f1) #{'foo': 1, 'bar': 2}
    print vars(f2) #{'cheese': 'stilton', 'foo': 3, 'bar': 4}
    

提交回复
热议问题