MCPcopy Create free account
hub / github.com/ipython/ipython / dir2

Function dir2

IPython/utils/dir2.py:23–51  ·  view source on GitHub ↗

dir2(obj) -> list of strings Extended version of the Python builtin dir(), which does a few extra checks. This version is guaranteed to return only a list of true strings, whereas dir() returns anything that objects inject into themselves, even if they are later not really vali

(obj)

Source from the content-addressed store, hash-verified

21
22
23def dir2(obj):
24 """dir2(obj) -> list of strings
25
26 Extended version of the Python builtin dir(), which does a few extra
27 checks.
28
29 This version is guaranteed to return only a list of true strings, whereas
30 dir() returns anything that objects inject into themselves, even if they
31 are later not really valid for attribute access (many extension libraries
32 have such bugs).
33 """
34
35 # Start building the attribute list via dir(), and then complete it
36 # with a few extra special-purpose calls.
37
38 try:
39 words = set(dir(obj))
40 except Exception:
41 # TypeError: dir(obj) does not return a list
42 words = set()
43
44 if safe_hasattr(obj, '__class__'):
45 words |= set(dir(obj.__class__))
46
47 # filter out non-string attributes which may be stuffed by dir() calls
48 # and poor coding in third-party modules
49
50 words = [w for w in words if isinstance(w, str)]
51 return sorted(words)
52
53
54def get_real_method(obj, name):

Callers 6

dict_dirFunction · 0.90
test_baseFunction · 0.90
test_SubClassFunction · 0.90
attr_matchesMethod · 0.90

Calls 1

safe_hasattrFunction · 0.85

Tested by 4

test_baseFunction · 0.72
test_SubClassFunction · 0.72