Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:07:22+00:00 2026-06-07T05:07:22+00:00

>>> import module1 >>> dir(module1) [‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’] I try putting some

  • 0
>>> import module1
>>> dir(module1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__']

I try putting some functions like this to module1 code:

def __dir__(self):
    return 'ok'
def __dir__():
    return 'ok'
def __dir__(self):
    print 'ok'
def __dir__():
    print 'ok'

… but nothing of those works. How good writen overwriting dir function should look?

I want something like this:

>>> import module1
>>> dir(module1)
'ok' [or ok]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:07:24+00:00Added an answer on June 7, 2026 at 5:07 am

    Your question is asking how to modify the behavior of dir() on module objects, but in your comments you clarified that your ultimate goal is to modify the behavior of help(module). dir works differently on different objects, and I don’t believe there is a direct way to change that for a module. It will simply always want to list the attributes of the module being everything in the scope.

    What you can do though, is define an __all__ attribute in your module, and specify which attributes should be made public in the help doc:

    mymodule

    __all__ = ['foo']
    
    foo = 'bar'
    
    class Klass(object):
        pass
    
    >>> import mymodule
    >>> help(mymodule)
    

    You will see that because Klass was excluded from __all__ it will not be visible in the help.

    Also, if you do from mymodule import *, only the foo attribute will be imported:

    >>> from mymodule import *
    >>> dir()
    # ['__builtins__', '__doc__', '__name__', '__package__', 'foo']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import os try: os.path.exists(E:/Contact) #Check if dir exist except: os.mkdir(E:/Contact) #if not, create def
Suppose you have these modules: module1.py import module2 def a(): module1.b() def c(): print
I have installed Numpy using ActivePython and when I try to import numpy module,
When I try to import the scipy module (version 0.11.0b1) in the Python interpreter
I thought import sys sys.path.append(/home/me/mydir) is appending a dir to my pythonpath if I
I have this simple code here import os path = (raw_input(Enter dir: )) f
I have a directory structure like this... dir/ build.py dir2 dir3/ packages.py Now the
i am trying to use scapy in python while i try to import the
import os print __file__ print os.path.dirname(__file__) os.chdir('/tmp') print __file__ # unchanged, of course print
tests.py import unittest import sys import os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) import my_module.ext print sys.path print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.