Skip to content

Instantly share code, notes, and snippets.

@math2001
Created November 26, 2016 23:00
Show Gist options
  • Save math2001/0c4a47aacb29d44e9f24a0689f85a186 to your computer and use it in GitHub Desktop.
Save math2001/0c4a47aacb29d44e9f24a0689f85a186 to your computer and use it in GitHub Desktop.
A better stdclass for python: save the class name
def StdClass(name='Unknown'):
return type(name.title(), (), {})
@math2001
Copy link
Author

>>> line = StdClass('line')
>>> line.text = 'Hello world!'
>>> line.pos = [5, 5 + len(line.text)]
>>> line
<class '__main__.Line'>
>>> line.error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Line' has no attribute 'error'

You get a easy-to-understand traceback (Line has no..., not StdClass has no... or whatever. You choose the name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment