gpt4 book ai didi

javascript - 在 Python 中,您可以像在 JavaScript 中那样对字符串函数进行原型(prototype)设计吗?

转载 作者:太空狗 更新时间:2023-10-30 01:51:42 24 4
gpt4 key购买 nike

<分区>

在 JavaScript 中,你可以这样做来为现有的原型(prototype)函数添加一个 trim 函数

String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");

然后你可以做这样的事情来访问这些功能。

trimmed = myString.trim();

而且你自己有一个 trim 过的字符串。

在 python 中,我知道如何通过这样做列出字符串的原型(prototype)函数:

print dir(myString)

但是如何在 Python 中添加原型(prototype)函数呢?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com