निम्नलिखित मॉड्यूल को देखते हुए:पाइथन निरीक्षण क्यों करता है। यह सोचता है कि एक उदाहरण एक वर्ग है?
class Dummy(dict):
def __init__(self, data):
for key, value in data.iteritems():
self.__setattr__(key, value)
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__=dict.__setitem__
__delattr__=dict.__delitem__
foo=Dummy({"one":1, "two":2})
foo
inspect.getmembers(..., predicate=inspect.isclass)
के आउटपुट में क्यों दिखता है?
$ python2.5
Python 2.5.2 (r252:60911, Aug 28 2008, 13:13:37)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import junk
>>> import inspect
>>> inspect.getmembers(junk, predicate=inspect.isclass)
[('Dummy', <class 'junk.Dummy'>), ('foo', {'two': 2, 'one': 1})]
>>> inspect.isclass(junk.foo)
True
मुझे उम्मीद है कि inspect
केवल Dummy
लौटाएगा क्योंकि यह मॉड्यूल में एकमात्र क्लास परिभाषा है। जाहिर है, हालांकि, junk.foo निरीक्षण मॉड्यूल की आंखों में एक कक्षा है। ऐसा क्यों है?
विशेष रूप से 'AttributeError' की कमी जो वास्तव में शानदार __horrible__ कीड़े का कारण बन जाएगा। – katrielalex
धन्यवाद। आपकी व्याख्या पूरी समझ में आता है। –