I've just implemented a custom non-punctuated and purely alphabetic string object in Python 3.4:
from string import ascii_letters
class astr(str):
"""
astr is a custom non-punctuated and purely alphabetic string
that returns a string of zeros if an impure string is provided.
"""
def __new__(self, content):
if (lambda s: True if False not in [False for c in s if c not in ascii_letters] else False)(content):
return str.__new__(self, content)
else:
return str.__new__(self, ''.join([str(0) for c in content]))
x = astr("Normalstring")
y = astr("alphanumeric7234852fsd36asd4fgh232")
z = astr("Ácçènts")
print(x)
print(y)
print(z)
Is it possible to "hack" the built-in object in such a way that I just use:
x = "Normalstring"
?
I'm aware that ruby allows that kind of trickery, but I'm not a Python wizard, so, perhaps there's some black magick trick that I'm not aware of that allows one to do it (Or is the __ new __ magic method the ultimate "hacking limit"?)
I'm not doing anything useful, by the way. I'm just curious.
EDIT:
My question is more specific, and more importantly, it asks why. Perhaps someone knows why the developers of Python chose to do it that way.