Created
February 21, 2018 03:33
-
-
Save allenyang79/29220d35e2b9de38089882524e11a55a to your computer and use it in GitHub Desktop.
python custom ErrorMsg inherit form str
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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