推荐学习书目
› 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
aminic
V2EX  ›  Python

Python 怎么获取类定义时字段的顺序?

  •  
  •   aminic · May 7, 2017 · 3678 views
    This topic created in 3428 days ago, the information mentioned may be changed or developed.
    In [4]: class Person:
    ...: pass



    In [5]: class Friend:
    ...: zhang_san=Person()
    ...: wang_wu=Person()
    ...: li_si=Person()

    Friend.__dict__ 或 dir(Friend)得到的字段列表都是按照码表正序排列的,有没有办法可以从类定义中得到静态字段定义时的顺序么,或者在静态字段定义时可以得到类的信息?即 Person 中可以得到关联的 Friend 类信息
    4 replies  •  2017-05-09 22:41:57 +08:00
    aminic
        1
    aminic  
    OP
       May 7, 2017
    或者静态字段如何从内部知道自己是某类的成员?
    比如 zhang_san=Person()
    在 Person()内,就可以得到 Friend.zhang_san 这样的信息。。
    aminic
        2
    aminic  
    OP
       May 7, 2017   ❤️ 2
    carlonelong
        3
    carlonelong  
       May 8, 2017
    你用 dict 还指望有序呢。。
    aminic
        4
    aminic  
    OP
       May 9, 2017
    @carlonelong 是啊,当时傻 X 了。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2560 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:28 · PVG 23:28 · LAX 08:28 · JFK 11:28
    ♥ Do have faith in what you're doing.