Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created February 21, 2018 03:33
Show Gist options
  • Save allenyang79/29220d35e2b9de38089882524e11a55a to your computer and use it in GitHub Desktop.
Save allenyang79/29220d35e2b9de38089882524e11a55a to your computer and use it in GitHub Desktop.
python custom ErrorMsg inherit form str
"""Partice to custom string inherit python str
"""
class ErrorMsg(str):
def __new__(cls, *args, **kw):
if len(args) > 0:
message = args[0]
else:
message = kw.get('message')
return str.__new__(cls, message)
def __init__(self, message, payload=None):
print "init", message, payload
self._message = message
self._payload = payload
def __str__(self):
return self._message
msg = ErrorMsg("oops", {"a": "A" , "b": "B"})
print msg
print "test: %s" % msg
print "test: " + msg
print msg._payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment