Last active
December 10, 2015 08:48
-
-
Save mahmoud/4409907 to your computer and use it in GitHub Desktop.
This must be the result of an optimization of some sort? If you inherit from str or unicode and override the __init__, the __init__ will get called, but it's subject to the exact same signature as the base str or unicode type. Probably just need to override __new__() since str/unicodes are supposed to be immutable anyhoo. (PS, have I always had …
This file contains 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
class TestStr(str): | |
def __init__(self, a, b=True): | |
if b: | |
print(b'using custom init for sure') | |
super(TestStr, self).__init__(a) | |
>>> core.TestStr('hi') | |
using custom init for sure | |
'hi' | |
>>> core.TestStr('hi', False) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: str() takes at most 1 argument (2 given) | |
class TestUnicode(unicode): | |
def __init__(self, a, b=True): | |
if b: | |
print('using custom init for sure') | |
super(TestUnicode, self).__init__(a) | |
>>> core.TestUnicode('hi') | |
using custom init for sure | |
u'hi' | |
>>> core.TestUnicode('hi', True) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unicode() argument 2 must be string, not bool | |
>>> core.TestUnicode('hi', 'wat') | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
LookupError: unknown encoding: wat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment