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 7878297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:37:09+00:00 2026-06-03T03:37:09+00:00

I want to assign a callable class as a function on another class, and

  • 0

I want to assign a callable class as a function on another class, and then reference the owner class from the owned function:

class DoIt(object):

    def __call__(self):
        print "%s did it" % self.name

class Doer(object):

    def __init__(self, name):
        self.name = name

    doSomething = DoIt()


doer = Doer("Bob")
doer.doSomething()

In the code above, I want the doSomething() function to print “Bob did it”, but with this code I’ll get a

AttributeError: ‘DoIt’ object has no attribute ‘name’

Because self references the DoIt instance, not the Doer instance. Is it possible to reference the Doer instance?

  • 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-03T03:37:10+00:00Added an answer on June 3, 2026 at 3:37 am
    class DoIt(object):
    
        def __get__(self, instance, owner):
            def done():
                print "%s did it" % instance.name
            return done
    
    class Doer(object):
    
        def __init__(self, name):
            self.name = name
    
        doSomething = DoIt()
    
    
    doer = Doer("Bob")
    doer.doSomething()
    

    Explanation: (This is the asker explaining this answer as best he can)

    Python offers you descriptor classes, which are classes that act as attributes (in contrast to attributes as instance variables or properties). The __get__ method of a descriptor class dictates what’s returned when that class is assigned and accessed as an attribute on another class. And __get__ lets you access both the owning instance and owning class (See method signature).

    By having __get__ return a function, you essentially create a callable that has access to its owner.

    For more on descriptors, see Attributes, Properties and Descriptors- Building Skills in Python

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign a function implementation dynamically. Let's start with the following: class
I want to assign a class attribute via a string object - but how?
I want to assign value to global variable in javascript from jquery ajax function.
Let's say I create an instance of a class and want to assign some
I want to assign the value obtained from the concatenation of these two variables
I want to assign a class to the list-items of an ul li list
I want to assign an object to volatile object in the same type, but
I want to assign something a value and then try and get someone to
I have a list and I want to assign a random element from that
I want to assign string from command line. But I got segmentation fault. why?

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.