可以在Python中更改内置对象吗?若否,原因为何?

可以在Python中更改内置对象吗?若否,原因为何?,python,class,overriding,Python,Class,Overriding,我刚刚在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 __

我刚刚在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)
是否有可能以我仅使用的方式“破解”内置对象:

x = "Normalstring"
?

我知道ruby允许这种把戏,但我不是Python向导,所以,也许有一些我不知道的黑色魔法把戏可以让你这么做(或者“新”魔法方法是终极“黑客限制”?)

顺便说一句,我没有做任何有用的事。我只是好奇

编辑:

我的问题更具体,更重要的是,它问为什么。也许有人知道Python的开发人员为什么选择这样做。

TypeError:无法设置内置/扩展类型“str”的属性
否。

类型错误:无法设置内置/扩展类型“str”的属性

不。

不,很遗憾,您不能。当然,可能是重复的,请在您的计算机上编辑Python源代码。这是个好主意吗?可能不会。不,不幸的是你不能。当然,可以在你的机器上编辑Python源代码。这是个好主意吗?可能不会。不,不幸的是你不能。当然,可以在你的机器上编辑Python源代码。这是个好主意吗?可能不会。