First, keep in mind that you're really not saving much space—and you could save a lot more space, and make your style more Pythonic, by just temporarily binding the object to a shorter name:
_ = obj
_.attr1 = 'foo'
_.attr2 = 'bar'
Compare that to VB-ish syntax:
vbwith obj:
.attr1 = 'foo'
.attr2 = 'bar'
You're really only typing and reading one extra character per line. Is that too much of a burden?
This wouldn't work in languages with C-style variables with value-copying semantics, because you'd just be setting attributes on a copy of obj. But in Python, a = b makes a into another name for the value that b names, it doesn't copy anything. (If you're coming from a C background, it may help to think of this as _ = &obj followed by a bunch of lines starting _->.)
If you really want to do this, the safest way is to write update_attr as a loop around setattr:
def update_attrs(obj, **attrs):
for attr, value in attrs.items():
setattr(obj, attr, value)
update_attr(obj,
attr1='foo',
attr2='bar'
)
The reason you want to use setattr rather than modifying the object's __dict__ or vars() is that not all attributes are stored in the object's __dict__. They may be stored in __slots__, or @property descriptors on the class, or get-set methods on a C extension object, or some custom thing provided by the metaclass. So, modifying the __dict__ may fail in some cases, or may appear to work but do nothing, or, worst of all, do the wrong thing in a way that's very hard to tell (like hiding a property instead of setting it). setattr is guaranteed to work whenever direct access would work, and to fail whenever direct access would fail.
While much of the Python community thinks actual VB-style with syntax is a bad thing (although I can't find the link to Guido's blog post about it), your more explicit syntax seems readable and not at all misleading to me, so if you really want to use this function, go ahead. But again, I don't think there's much need.
obj.__dict__.update({'attr1': 'foo', 'attr2': 'bar'})__dict__though...obj.__dict__. Another issue is that the object you are working with might rely on__setattr__for its functionality, and if you updateobj.__dict__directly then you are bypassing that functionality (libraries like SQLAlchemy come to mind).vars(obj).update(dct), but for the reasons that others have already mentioned, I would avoid this idiom unless you actually know the object that you're working with pretty well (e.g. inside a class method where you're adding attributes toself)