比如 a = "abcd"
def format(str):
....print ','.join(list(str))
然后添加一个方法 a.format() ?
def format(str):
....print ','.join(list(str))
然后添加一个方法 a.format() ?
|
1
builtin类型不知道怎么搞
a = "abcd" def format(self, str): ....print ",".join(list(str)) base = type("str", [type(a), ], {}) base.format = format a = base(a) a.format("123") |
|
2
@binux 这样的话就不用这么麻烦了,直接新建一个 class 就可以了
>>> class cls: ... def __init__(self, value): ... self.value = value ... ... def format(self): ... print ','.join(self.value) ... ... ... >>> a=cls('sab') >>> a.format() s,a,b |
|
3
Python的builtin类型是无法改的,因为__dict__是只读的,只有自定义类型可以。
|
|
alsotangOct 29, 2012
4
用 Ruby 的话来说,叫做 “Python 的buildin 类型无法打开。”
|