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

  • Home
  • SEARCH
  • 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 3321770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:06:10+00:00 2026-05-17T23:06:10+00:00

I am trying to dynamically assign a function to __del__ of an instance of

  • 0

I am trying to dynamically assign a function to __del__ of an instance of a class so that it gets called when using dir() on that object. I need __dir__ to be unique for each instance of the class. As a stripped down example, I have tried:

import types
class Foo(object):
    def __init__(self, arg):
        def __dir__(self):
            print "in __dir__"
            return [arg]
        self.__dir__ = types.MethodType(__dir__, self, self.__class__)

foo = Foo('bar')
print dir(foo)
print
print foo.__dir__()

This prints:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

in __dir__
['bar']

If I instead do this:

class Foo(object):
    def __dir__(self):
        print "in __dir__"
        return ['bar']

foo = Foo()
print dir(foo)

that outputs:

in __dir__
['bar']

as expected, but can not be customized for each instance of the class. Any ideas?

  • 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-05-17T23:06:11+00:00Added an answer on May 17, 2026 at 11:06 pm

    Ok, based on your comment I worked things a bit. I think this might be closer to the behavior you’re looking for. Calling dir with no args gives all the names in the local scope and calling it on __class__ gives all class names. This ignores the defined __dir__ which can be called later on. I’m curious what you’re using this for, maybe there is a simpler way to get the intended behavior?

    class Foo(object):
        def __init__(self, arg=None):
            self.arg = arg
            print dir(self.__class__) + dir()
    
        def __dir__(self):
            return [self.arg()]
    
    
    def dirfoo():
        return ["new thing"]
    
    foo = Foo(dirfoo)
    
    print dir(foo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically define functions that call through to another function that takes
I've been trying to assign a function to onclick event of a dynamically created
Question I am trying to dynamically get the default for a type that is
Trying to make a make generic select control that I can dynamically add elements
I trying to export an HTML table named Table that is dynamically binded to
I need to create a structure inside a function (dynamically with malloc) Then i
Following this nice example I found, I was trying to create a function that
Currently, I'm trying to make a function that sorts a vector full of fighters
So I'm trying to figure out if there is some method to dynamically create/assign
I am trying to use batch scripting to create and assign variables dynamically based

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.