जब तक आप केवल अजगर 3.x में रुचि रखते हैं के रूप में अनुमान लगाओ कि आप हो सकते हैं), तो वास्तव में सजावटकर्ताओं के बिना ऐसा करने का एक आसान तरीका है: पायथन 3 आपको कक्षा के परिभाषित होने पर उपयोग करने के लिए ऑब्जेक्ट जैसे अपना शब्दकोश प्रदान करने की अनुमति देता है।
निम्नलिखित कोड लाइनों के पिछले कुछ के लिए छोड़कर PEP3115 से है जो मैं क्रम में बाहर तरीकों मुद्रित करने के लिए कहा:
# The custom dictionary
class member_table(dict):
def __init__(self):
self.member_names = []
def __setitem__(self, key, value):
# if the key is not already defined, add to the
# list of keys.
if key not in self:
self.member_names.append(key)
# Call superclass
dict.__setitem__(self, key, value)
# The metaclass
class OrderedClass(type):
# The prepare function
@classmethod
def __prepare__(metacls, name, bases): # No keywords in this case
return member_table()
# The metaclass invocation
def __new__(cls, name, bases, classdict):
# Note that we replace the classdict with a regular
# dict before passing it to the superclass, so that we
# don't continue to record member names after the class
# has been created.
result = type.__new__(cls, name, bases, dict(classdict))
result.member_names = classdict.member_names
return result
class MyClass(metaclass=OrderedClass):
# method1 goes in array element 0
def method1(self):
pass
# method2 goes in array element 1
def method2(self):
pass
x = MyClass()
print([name for name in x.member_names if hasattr(getattr(x, name), '__call__')])
अगर मुझे आदेश की परवाह नहीं है तो ऐसा करने का कोई तरीका? – curious
निश्चित रूप से, मैं जवाब संपादित कर दूंगा। –