Python equivalent to VB “with” block

前端 未结 4 413
北海茫月
北海茫月 2020-12-22 08:46

VB (and C99 and C#, actually) have a way to set multiple attributes on one object with a contracted syntax where you don\'t have to repeat the object name before \".\" . Is

4条回答
  •  执念已碎
    2020-12-22 09:37

    If your objective is just to avoid repetition and you are working with your own objects, you could use a fluent interface like this:

    obj.set_attr1('foo')
       .set_attr2('bar')
    

提交回复
热议问题