Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • Damnit - I spent two hours today writing up my own decorator to do this and then I found this. Anyways, posting mine because it handles default values while yours doesn't. stackoverflow.com/a/32448434/901641 Commented Sep 8, 2015 at 2:39
  • +1 for mentioning func_code. Started digging in that direction and found lots of interesting stuff there. Commented Mar 28, 2016 at 19:31
  • This'd be nicer as a class decorator though, allowing for something like @as_struct \n class MyStruct: \n a = somedefault \n b = somedefault where the decorator builds the real class based on the static varnames, with default assignment if the class is instantiated with one (or more) members missing. Having to decorate a function inside the class, and needing a pass ends up basically being the same amount of code as a normal class. Commented Dec 25, 2022 at 18:14