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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:58:32+00:00 2026-05-13T08:58:32+00:00

I wrote the following code trying to figure out how to instantiate the subclasses

  • 0

I wrote the following code trying to figure out how to instantiate the subclasses within the main class.. I came up with something that doesn’t feel right.. at least for me.

Is there something wrong with this type of instancing? Is there a better way to call subclasses?

class Family():
  def __init__(self):
    self.Father = self.Father(self)
    self.Mother = self.Mother(self)

  class Father():
    def __init__(self, instance = ''):
      self = instance if instance != '' else self
      print self

    def method(self):
      print "Father Method"

    def fatherMethod(self):
      print "Father Method"


  class Mother():
    def __init__(self, instance = ''):
      self = instance if instance != '' else self
      print self

    def method(self):
      print "Mother Method"

    def motherMethod(self):
      print "Mother Method"



if __name__ == "__main__":
  Family = Family()
  Family.Father.method()
  Family.Mother.method()
  • 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-13T08:58:32+00:00Added an answer on May 13, 2026 at 8:58 am

    What you’ve defined there are not (in Python terminology at least) subclasses – they’re inner classes, or nested classes. I’m guessing that this isn’t actually what you were trying to achieve, but I’m not sure what you did actually want – but here are my four best guesses:

    1. A subclass is where the class inheriting from another class is called a subclass. To make father a subclass of family, use the syntax class Father(Family):. What you’ve created here is actually called an Inner Class, not a subclass.

    2. When you see something like Family.Father.method(), it often means Family is a module and Father is a class in that module. In Python, module basically means .py file. A module doesn’t have an __init__ method, but all code at the top level of the module (such as the if __name__ ... line) gets executed when a module is imported.

    3. Similarly, you could make Family a package – which in Python basically means a directory on the filesystem containing an __init__.py file. Father and Mother would then be modules or classes within the package

    4. Possibly what you’re trying to achieve is declare that an object of type Family always has a Father object and a Mother object. This doesn’t require nested classes (in fact, nested classes are a completely bizarre way to do this). Just use:

    >>> class Mother():
    ...   def whoami(self):
    ...     print "I'm a mother"
    ... 
    >>> class Father():
    ...   def whoami(self):
    ...     print "I'm a father"
    ...
    >>> class Family():
    ...   def __init__(self):
    ...     self.mother = Mother()
    ...     self.father = Father()
    ...  
    >>> f = Family()
    >>> f.father.whoami()
    I'm a father
    >>> f.mother.whoami()
    I'm a mother
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.