0

Can we avoid built-ins showing up when dir is used on a module I created? Like, in this case, I want to avoid showing up of inbuilt libraries like os, sys, random, struct, time, hashlib etc.

>>> import endesive.pdf.cms
>>> dir(endesive.pdf.cms)
['EncodedString', 'SignedData', 'UnencryptedBytes', 'WNumberObject', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_cmath', 'backends', 'codecs', 'datetime', 'hashlib', 'io', 'pdf', 'pkcs12', 'po', 'random', 'sign', 'signer', 'struct', 'sys', 'time']

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Does this answer your question? [Is there a Python method to access all non-private and non-builtin attributes of a class?](https://stackoverflow.com/questions/17075071/is-there-a-python-method-to-access-all-non-private-and-non-builtin-attributes-of) – Torxed May 10 '20 at 09:11
  • I am asking about ways to avoid showing up of the inbuilt library names that I have imported when a client fires dir on my module – Prince Roshan May 10 '20 at 09:26
  • 1
    You're asking **A** module, not **your** module. It's quite a big difference. One implies your things that you implement, the other on any given module. – Torxed May 10 '20 at 09:33
  • OK my bad, but you can create a dummy module with some inbuilt library imports, and let's say your class and check for it. – Prince Roshan May 10 '20 at 09:38
  • I changed your titel and question details accordingly. – Torxed May 10 '20 at 09:53

1 Answers1

0

State this in module:

import sys
# Your code...
def __dir__():
    return [x for x in globals() if x not in sys.modules]
wyz23x2
  • 298
  • 4
  • 16
  • I am asking about ways to avoid showing up of the inbuilt library names that I have imported when a client fires dir on my module. – Prince Roshan May 10 '20 at 09:21
  • Following error pops up when adding your code in my module namespace. dir(endesive.pdf.cms) Traceback (most recent call last): File "", line 1, in TypeError: 'list' object is not callable – Prince Roshan May 11 '20 at 08:02
  • What version are you using? I don't see a place that calls a ``list``? – wyz23x2 May 11 '20 at 13:34
  • Using python 3.7.7 – Prince Roshan May 11 '20 at 13:52
  • P.S. You should add that line at the end of the module, or ``globals()`` will not be right. Also, I discovered that stackoverflow might not keep the __s -- check if you entered ``dir`` instead of ``__dir__``. – wyz23x2 May 12 '20 at 02:22
  • Hey man, __dir__ is a magic **method**. you are not supposed to assign a list to it correct me if I am wrong. you just have to override the method in your namespace. it's working now. – Prince Roshan May 12 '20 at 04:18
  • Also, One more question have you used dir before, and for what purpose you have used it. I really want to know. – Prince Roshan May 12 '20 at 04:24
  • @PrinceRoshan I don't use ``dir`` as much. I only use it to check if I misspelled something (e.g. checking if some name similar is in ``dir(module)``). – wyz23x2 May 12 '20 at 12:47