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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:20:46+00:00 2026-05-13T17:20:46+00:00

Consider the following Python (runs in 2.x or 3.x): class Outer(object): pass class Inner(object):

  • 0

Consider the following Python (runs in 2.x or 3.x):

class Outer(object):
  pass

  class Inner(object):
    def __init__(self):
      print("Inner.self", self)

o = Outer()
i = o.Inner()

I want to get my hands on o while inside Inner.__init__(). But:

  • I don’t want o to be an explicit parameter to Inner.
  • I want O.Inner and o.Inner to be a class object, not something weird like a closure.

Can you suggest how I might achieve this?

Right now my best idea is to use thread local storage. In my use case, whenever I construct an o.Inner(), I’m already inside a method on o somewhere, and it wouldn’t be a big deal to add

threading.local()["my o object"] = o

to my code.

This gives you an idea of the level of depravity I’m willing to consider.

  • 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-13T17:20:46+00:00Added an answer on May 13, 2026 at 5:20 pm

    In Python 2.6, a class decorator that’s also a custom descriptor matches the specs you give:

    class InnerClassDescriptor(object):
      def __init__(self, cls):
        self.cls = cls
      def __get__(self, instance, outerclass):
        class Wrapper(self.cls):
          outer = instance
        Wrapper.__name__ = self.cls.__name__
        return Wrapper
    
    class Outer(object):
      @InnerClassDescriptor
      class Inner(object):
        def __init__(self):
          print self.outer
    
    o = Outer()
    i = o.Inner()
    print 'Outer is a', type(Outer)
    print 'Inner is a', type(o.Inner)
    

    This emits:

    <__main__.Outer object at 0x82f90>
    Outer is a <type 'type'>
    Inner is a <type 'type'>
    

    just to confirm that

    o.Inner [[is]] a class object, not
    something weird like a closure

    as per your peculiar specs. Of course it needs to be a different class each time for reentrancy — even in a single-threaded world, the following:

    o1 = Outer()
    o2 = Outer()
    i1 = o1.Inner
    i2 = o2.Inner
    print i1(), i2(), i1(), i2()
    

    should work cleanly, and stashing o1 vs o2 anywhere else than in the classes returned by o1.Inner vs o2.Inner (e.g., in TLS) would mean horrible results for this use.

    But then you didn’t specify “o.Inner has to be exactly the same class object for every possible o that’s an instance of Outer“, so this code fully meets the specs you did give;-).

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

Sidebar

Ask A Question

Stats

  • Questions 422k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Grails ORM is built on Hibernate, so my guess is… May 15, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer No. That's the point of absolutes is that they don't… May 15, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer Use the Name attribute instead of the id attribute. The… May 15, 2026 at 11:21 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.