比如 a = "abcd"
def format(str):
....print ','.join(list(str))
然后添加一个方法 a.format() ?
def format(str):
....print ','.join(list(str))
然后添加一个方法 a.format() ?
4 条回复 • 1970-01-01 08:00:00 +08:00
|
1
binux 2012 年 10 月 28 日 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
Brutal OP @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
timonwong 2012 年 10 月 28 日 Python的builtin类型是无法改的,因为__dict__是只读的,只有自定义类型可以。
|
|
4
alsotang 2012 年 10 月 29 日
用 Ruby 的话来说,叫做 “Python 的buildin 类型无法打开。”
|
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide