== Initializing Python struct objects with optional defaults Recently I was writing code to register names and their attributes. There were enough attributes that I didn't want to specify all of them all of the time, so I did the obvious Python thing: I made the _register()_ function take a bunch of keyword arguments that had default values. The attributes are stored with a [[_struct_ object|EmulatingStructsInPython]], because I wanted an _attrs.attribute_ syntax for accessing them. The straightforward way to initialize the struct object was to write '_vi = ViewInfo(factory = factory, onDir = onDir, ...)_', but that sort of repetition is annoying, especially when I had a perfectly good set of name/value pairs in the form of _register()_'s arguments. If only I could get at them. It turns out that you can use the _locals()_ dictionary for this, *if* you use it before you set any local variables in the function. So: > class ViewInfo(Struct): > pass > > def register(name, factory, onDir = False, \ > onFile = True, ...): > vi = ViewInfo(**locals()) > view_dict[name] = vi (I did not strictly need the _name_ attribute in the ViewInfo data, but it doesn't do any harm and it meant I could use _locals()_ straight.) A similar pattern can be done directly in a _struct_ class as: > class ViewInfo: > def __init__(self, name, factory, \ > onDir = False, ...): > for k, v in locals().items(): > if k != "self": > setattr(self, k, v) (You really want to exclude _self_, since circular references make the garbage collector work harder than necessary.)