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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:09:05+00:00 2026-05-12T00:09:05+00:00

I have two classes that refer to each other, but obviously the compiler complains.

  • 0

I have two classes that refer to each other, but obviously the compiler complains. Is there any way around this?

EDIT

Actually my code is slightly different than what Hank Gay uses. So python can definitely deal with some kinds of circular references, but it tosses an error in the following situation. Below is what I’ve got and I get an ‘name Y not defined error’

class X(models.Model):

        creator = Registry()
        creator.register(Y)

class Y(models.Model):
    a = models.ForeignKey(X)
    b = models.CharField(max_length=200)

Hope this helps clarify. Any suggestions.

  • 1 1 Answer
  • 2 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-12T00:09:05+00:00Added an answer on May 12, 2026 at 12:09 am

    In python, the code in a class is run when the class is loaded.

    Now, what the hell does that mean? 😉

    Consider the following code:

    class x:
        print "hello"
        def __init__(self): print "hello again"
    

    When you load the module that contains the code, python will print hello. Whenever you create an x, python will print hello again.

    You can think of def __init__(self): ... as equivalent with __init__ = lambda self: ..., except none of the python lambda restrictions apply. That is, def is an assignment, which might explain why code outside methods but not inside methods is run.

    When your code says

    class X(models.Model):
        creator = Registry()
        creator.register(Y)
    

    You refer to Y when the module is loaded, before Y has a value. You can think of class X as an assignment (but I can’t remember the syntax for creating anonymous classes off-hand; maybe it’s an invocation of type?)

    What you may want to do is this:

    class X(models.Model):
        pass
    class Y(models.Model):
        foo = something_that_uses_(X)
    X.bar = something_which_uses(Y)
    

    That is, create the class attributes of X which reference Y after Y is created. Or vice versa: create Y first, then X, then the attributes of Y which depend on X, if that’s easier.

    Hope this helps 🙂

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

Sidebar

Related Questions

I have two classes that each need an instance of each other to function.
I have two basic classes that inherits each other: BaseClass: // .h @interface BaseClass
If I have two classes that both implement an interface, but also inherit, do
I have two classes that work seperate from another, but they extend the same
Let's say you have two classes that extend UserControl . Each of the controls
I have the following issue. I have two classes that manipulate information but they
I have two classes that extend the activity class. Each class has it's own
I have two classes that I would like to merge into a composite. These
I have two classes that I am testing (let's call them ClassA and ClassB).
I have two classes that represent two different database entities. Their relationship is 1:m

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.