2008-11-20 17 views
8

मेरे पास एक ऐसा फ़ंक्शन है जो पैरामीटर के रूप में एक और कार्य करता है। यदि समारोह किसी वर्ग का सदस्य है, तो मुझे उस वर्ग का नाम ढूंढना होगा। जैसेपायथन में, आप सदस्य फ़ंक्शन की कक्षा का नाम कैसे प्राप्त कर सकते हैं?

def analyser(testFunc): 
    print testFunc.__name__, 'belongs to the class, ... 

मैंने सोचा था कि

testFunc.__class__ 

मेरी समस्या को हल किया है, लेकिन वह सिर्फ मुझे है कि testFunc एक समारोह है बताता है।

उत्तर

11
testFunc.im_class 

https://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy

im_class is the class of im_self for bound methods or the class that asked for the method for unbound methods

+0

टिप: जब आपको ऐसी समस्या हो, तो आप अपने दुभाषिया में "डीआईआर" का उपयोग कर सकते हैं यह देखने के लिए कि कौन सी विधि testFunc है, या बेहतर: ipython टैब पूर्णता मदद करता है! –

+0

मैं हमेशा अपने reponses पर एक पल बहुत देर हो चुकी हूँ। मैं बस नए दस्तावेज़ों में उपयोगकर्ता द्वारा परिभाषित विधियों stanza का पता लगाना प्रतीत नहीं कर सका। +1 – JimB

+0

धन्यवाद; आपके समाधान के उदाहरण के लिए आपका समाधान ठीक काम करता है। दुर्भाग्य से मेरे लिए मैंने अपनी वास्तविक समस्या को बहुत सरल बना दिया था। मैंने इसे किसी अन्य प्रश्न में उठाया है:

5

मैं पाइथन विशेषज्ञ नहीं हूं, लेकिन क्या यह काम करता है?

testFunc.__self__.__class__ 

यह बाध्य तरीकों के लिए काम करने के लिए लगता है, लेकिन आपके मामले में, आप एक अबाध विधि का उपयोग किया जा सकता है, ऐसी स्थिति में इस बेहतर कार्य कर सकता:

testFunc.__objclass__ 

यहाँ परीक्षण मैं प्रयोग किया जाता है:

Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import hashlib 
>>> hd = hashlib.md5().hexdigest 
>>> hd 
<built-in method hexdigest of _hashlib.HASH object at 0x7f9492d96960> 
>>> hd.__self__.__class__ 
<type '_hashlib.HASH'> 
>>> hd2 = hd.__self__.__class__.hexdigest 
>>> hd2 
<method 'hexdigest' of '_hashlib.HASH' objects> 
>>> hd2.__objclass__ 
<type '_hashlib.HASH'> 

अरे हाँ, एक और बात:

>>> hd.im_class 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'builtin_function_or_method' object has no attribute 'im_class' 
>>> hd2.im_class 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'method_descriptor' object has no attribute 'im_class' 

तो यदि आप कुछ बुलेटप्रूफ चाहते हैं, तो इसे __objclass__ और __self__ को भी संभालना चाहिए। लेकिन आपका माइलेज भिन्न हो सकता है।

+0

नहीं, यह आप संदेश देता है। –

+0

__objclass__ विशेषता का प्रयास करें और देखें कि क्या यह काम करता है। यदि ऐसा होता है, तो आपका फ़ंक्शन अनबाउंड है। –

3

उदाहरण तरीकों विशेषताओं होगा .im_class .im_func .im_self

http://docs.python.org/library/inspect.html#types-and-members

आप शायद अगर समारोह hasattr .im_class देखते हैं, और प्राप्त करना चाहते हैं वहां से कक्षा की जानकारी।

4

पायथन 3.3, .im_class से चला गया है। आप इसके बजाय .__qualname__ का उपयोग कर सकते हैं। यहाँ इसी पीईपी है: "AttributeError:: 'समारोह' ऑब्जेक्ट कोई विशेषता '__self__' है" https://www.python.org/dev/peps/pep-3155/

class C: 
    def f(): pass 
    class D: 
     def g(): pass 

print(C.__qualname__) # 'C' 
print(C.f.__qualname__) # 'C.f' 
print(C.D.__qualname__) #'C.D' 
print(C.D.g.__qualname__) #'C.D.g' 
संबंधित मुद्दे