V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
sudo987
V2EX  ›  Python

<type 'list'>

  •  
  •   sudo987 · 2016-07-14 21:03:24 +08:00 · 2668 次点击
    这是一个创建于 2814 天前的主题,其中的信息可能已经有所发展或是发生改变。

    编译器输入list,返回<type 'list'>

    请问这样的输出是怎么样做到的,如何自己写一个类可以有类似的输出,比如自己写的类

    class A(object):
        pass
    
    >>> A
    <class '__main__.A'>
    
    8 条回复    2016-07-15 08:09:03 +08:00
    sudo987
        1
    sudo987  
    OP
       2016-07-14 21:08:07 +08:00
    我能想到的是 list 类中存在元类,元类中的__str___和__repr__输出了这样的结果,不知道怎么看 list 的源码,请高手告知,谢谢。
    sudo987
        2
    sudo987  
    OP
       2016-07-14 21:17:29 +08:00
    @sudo987 试了下, list 中并没有__metaclass__ 这个属性,应该不是元类的问题,有高手知道怎么看 list 的源码么?
    lowzoom
        3
    lowzoom  
       2016-07-14 21:20:52 +08:00
    没有明白楼主在问什么
    baocaixiong
        4
    baocaixiong  
       2016-07-14 21:32:25 +08:00
    >>> list
    <type 'list'>
    >>> print list
    <type 'list'>
    >>>
    shyling
        6
    shyling  
       2016-07-14 23:13:33 +08:00 via iPad
    print 默认调用__str__,没有则调用__repr__,builtin 的东西要看 python 源码
    nealfeng
        7
    nealfeng  
       2016-07-14 23:30:28 +08:00
    python 3.5.1:

    >>> list
    <class 'list'>
    sudo987
        8
    sudo987  
    OP
       2016-07-15 08:09:03 +08:00 via iPhone
    @nealfeng 这…
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3861 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:08 · PVG 13:08 · LAX 22:08 · JFK 01:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.