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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:32:01+00:00 2026-05-19T09:32:01+00:00

I have a class1 which must be able to create an object of a

  • 0

I have a “class1” which must be able to create an object of a
different class-name. The class-name is passed as an argument called “friend”.
I want the “friend”-argument to default to a class-name called “class2”.

Additionally I need to have the same behavior for the class “class2”.
So “class2” should have “class1” as a default friend-argument:

class class1():
 def __init__(self, friend = class2):
  self.friendInstance = friend()

class class2():
 def __init__(self, friend = class1):
  self.friendInstance = friend()

class1()
class2()

Now i get the following error-message:

    def __init__(self, friend = class2):
NameError: name 'class2' is not defined

Of course, i can’t define class2 before class1 because
this would result in a similar error: “class1” is not defined.
Do you know a solution?

Thanks a lot for your help!

Henry

  • 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-19T09:32:01+00:00Added an answer on May 19, 2026 at 9:32 am

    You can push it to later:

    class class1(object):
        def __init__(self, friend=None):
            if friend is None:
                friend = class2
            self.friendInstance = friend()
    

    Edit: Actually, don’t do that. It will create a class2 instance that creates a class1 instance that creates a class2 instance, etc. Maybe you really want to pass an instance in instead of the class to be instantiated:

    class class1(object):
        def __init__(self, friend=None):
            if friend is None:
                self.friendInstance = class2(self)
            else:
                self.friendInstance = friend
    

    and likewise for class2. That isn’t as flexible, but it’s pretty simple. If you really want flexibility, you can do something like this:

    class class1(object):
        def __init__(self, friend=None, friendClass=None):
            if friend is None:
                self.friendInstance = (class2 if friendClass is None else friendClass)(self)
            else:
                self.friendInstance = friend
    
    class class2(object):
        def __init__(self, friend=None, friendClass=class1):
            if friend is None:
                self.friendInstance = friendClass(self)
            else:
                self.friendInstance = friend
    

    That could be simplified with inheritance or metaclasses, but you probably get the idea.

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

Sidebar

Related Questions

I have a library with a class called Recipient which has it's own fluent
I have this scenario, which I think must be pretty common: class Parameter {
I have a file, myfile.py , which imports Class1 from file.py and file.py contains
I have a class2 which is involved by class1 when clicks are made. I
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which constructor takes a Jakarta enums . I'm trying to

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.