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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:14:35+00:00 2026-05-23T17:14:35+00:00

In my code I have a class that contain a lot of functions and

  • 0

In my code I have a class that contain a lot of functions and a pair of classes (subclass I think). The main problem is that I want the subclasses to call an object, defined in the function init and I don’t know ho to referencing it

The main issue in code:

class principal:
    def __init__(self):
        self.a = object()
...

...
    class secondary(threading.Thread):
        def __init__(self):
            a.proceed() # problem !!!

I’ve tried to call a like principal.a.proceed() but the Traceback tells me:
AttributeError: class principal has no attribute ‘a’

how can I solve this. It appear that it is something easy to solve, but I can’t see it.

  • 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-23T17:14:35+00:00Added an answer on May 23, 2026 at 5:14 pm

    Unlike Java, there will not be a new principal.secondary class for each instance of principal. When you declare a class inside another class (a classeption :P) in Python it only means that the inside class will be accessed as a class attribute of the outside class and nothing more. So, there is no outside instance of principal to be accessed from the principal.secondary methods.

    Indeed, you want to do something meaningless in Python. I would suggest you do whatever you are trying to do by some other way. For example, you can get an instance of principal and pass it as parameter for the constructor of principal.secondary:

    p = principal()
    s = principal.secondary(p)
    

    Also, note that principal.secondary is not a subclass of principal but only a class inside a class. “Subclasses” are classes that extends/inherits from another one. So, principal.secondary is a subclass of threading.Thread, not from principal.

    Finally, two little off-topic suggestions: always make your classes extend object if it does not extend any other class and follow the PEP-8 by starting the name of your classes by an uppercase letter:

    class Principal(object):
        ...
    

    EDIT: If you really want to have a class bound to an instance like in Java (and I doubt you want),there is a way to emulate it. Create a function that receives an object as parameter, declare and return the class:

    def generateSecondary(outerObject):
        class Secondary(object):
            def __init__(self):
                outerObject.proceed()
        return Secondary
    

    The outerObject parameter will be bound to the class declared inside the function.

    Now, declare your Principal class. Call the generateSecondary() function in the __init__ method of Principal passing the initializing object as parameter and setting it to an attribute of the initializing object:

    class Principal(object):
        def __init__(self):
            self.attribute = "an attribute of %s" % self
            self.Secondary = generateSecondary(self)
    
        def proceed(self):
            print self.attribute
    

    The result is:

    >>> principal = Principal()
    >>> secondary = principal.Secondary()
    an attribute of <classeption.Principal object at 0x2e3970>
    >>> secondary
    <classeption.Secondary object at 0x2e3a10>
    

    It is possible because Python classes are first-class objects that can be bound to closures (in the traditional sense of “closure”). I do not recommend to use such code but there is a way to do what you asked – which may not be what you need.

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

Sidebar

Related Questions

I have code that looks like this: class T {}; class container { const
I have code that looks like this: template<class T> class list { public: class
I have a class, which is part of a code library project that was
I'm working on someone's code and they have a constructor that uses: class qwerty
I have this code that describes a service: public class navigation_web extends Service {
I have this code that will check for if the class more-results are in
I have following code snippet that i use to compile class at the run
I have this code that runs but never stops. class A { public static
I have a code piece that I am reviewing (using FindBugs ). public class
I have the following code that creates a serverside object of the xmlhttp class.

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.