"मदद" के लिए उपयोग किया जाने वाला पाठ वास्तव में "__doc__
" ऑब्जेक्ट की विशेषता है। मामला यह है कि आपके पास ऑब्जेक्ट के आधार पर, आप बस __doc__
विशेषता को सेट नहीं कर सकते हैं।
आपको क्या चाहिए है "help(object.attr)
" काम करने के लिए (और नहीं है कि help(object)
आप सभी संभव विशेषताओं दिखाता है) यह थोड़ा आसान है - आप केवल shure कि जो कुछ भी __getattr__
रिटर्न एक ठीक से सेट docstring hae करते मिलना चाहिए। ,
def __getattr__(self, attr):
if attr == "foo":
#function "foo" returns an integer
return foo()
...
आप बस समारोह "foo" वापसी होगी तो ही:
क्योंकि "यह काम नहीं कर रहा है" मुझे लगता था कि आप इस स्निपेट में जैसे कुछ समारोह कॉल के आंतरिक परिणाम लौट रहे हैं, इसे कॉल किए बिना, itś docstring सामान्य रूप से प्रदर्शित किया जाएगा।
क्या किया जा सकता एक डायनामिक रूप से तैयार वर्ग जो की वस्तु के रूप __getattr__
में वापसी मान रैप करने के लिए है एक उचित docstring शामिल हैं - हां, तो इस तरह somethong प्रयोग करके देखें:
def __getattr__(self, attr):
if attr == "foo":
#function "foo" returns an (whatever object)
result = foo()
res_type = type(result)
wrapper_dict = res_type.__dict__.copy()
wrapper_dict["__doc__"] = foo.__doc__ #(or "<desired documentation for this attribute>")
new_type = type(res_type.__name__, (res_type,), wrapper_dict)
# I will leave it as an "exercise for the reader" if the
# constructor of the returned object can't take an object
# of the same instance (python native data types, like int, float, list, can)
new_result = new_type(result)
elif ...:
...
return new_result
यह काम करना चाहिए - जब तक मुझे यह गलत लगता है कि क्यों हेल पहले स्थान पर काम नहीं कर रहा है - अगर ऐसा है, तो कृपया __getattr__
से लौटने वाले कुछ उदाहरण दें।
Thannks, यह मेरे मामले में काम करता है। __getattr__ में सभी attr देशी पायथन डेटा प्रकार देता है। –