推荐学习书目
› 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 · Jul 14, 2016 · 3829 views
    This topic created in 3724 days ago, the information mentioned may be changed or developed.

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

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

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

    >>> list
    <class 'list'>
    sudo987
        8
    sudo987  
    OP
       Jul 15, 2016 via iPhone
    @nealfeng 这…
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2533 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 07:45 · PVG 15:45 · LAX 00:45 · JFK 03:45
    ♥ Do have faith in what you're doing.