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
aminic
V2EX  ?  Python

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

  •  
  •   aminic · 2017-05-07 17:01:32 +08:00 · 2608 次点击
    这是一个创建于 2561 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复  ?  2017-05-09 22:41:57 +08:00
    aminic
        1
    aminic  
    OP
       2017-05-07 17:09:44 +08:00
    或者静态字段如何从内部知道自己是某类的成员?
    比如 zhang_san=Person()
    在 Person()内,就可以得到 Friend.zhang_san 这样的信息。。
    aminic
        2
    aminic  
    OP
       2017-05-07 17:28:13 +08:00   ?? 2
    carlonelong
        3
    carlonelong  
       2017-05-08 10:43:50 +08:00
    你用 dict 还指望有序呢。。
    aminic
        4
    aminic  
    OP
       2017-05-09 22:41:57 +08:00
    @carlonelong 是啊,当时傻 X 了。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1026 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:58 · PVG 03:58 · LAX 12:58 · JFK 15:58
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com