gpt4 book ai didi

python - 列出类的方法但不按字母顺序排序

转载 作者:行者123 更新时间:2023-11-28 22:48:20 25 4
gpt4 key购买 nike

dir()函数返回方法的列表,但是:

The resulting list is sorted alphabetically

我想知道哪些没有按字母顺序排序。我使用名称来帮助理解此方法的作用。我使用像 game_start、game_battle、game_map 这样的名称。我可以将名称更改为开始、战斗、 map ,但如果我想对 tkinter 应用程序中的所有其他方法使用相同的提交方法。所以我需要按声明顺序评估方法的名称。

还有其他解决方法吗?

例子:

class hi(object):
def __init__(self):
self.name="LLopis"
def question(self):
print("Question?")
def answer(self):
print("Answer")

dir(hi) 的结果是:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'answer', 'question']

我想要一个返回的函数(或者私有(private)方法的内部顺序,但按查找顺序):

['__init__', 'question', 'answer']

编辑:我不关心像 hi.name 这样的属性,因此尽管 this question是相关的,它不是完全重复的。

最佳答案

在执行程序时,解释器 (CPython) 不会保留类方法的声明顺序,因此除了查看源代码外,无法在运行时找到它。

附言但请参阅下面评论中@Jon Clements 的评论。

关于python - 列出类的方法但不按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com