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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:41:01+00:00 2026-06-12T00:41:01+00:00

I am defining two classes in the same module and want to use the

  • 0

I am defining two classes in the same module and want to use the second one in the first one (as a global variable):

class Class1(object):
    global_c2 = Class2()

    def foo(self):
        local_c2 = Class2()

class Class2(object):
    pass

global_c2 gets an error but local_c2 doesn’t. This makes sense because when the compiler looks through this file it won’t know that Class2 is going to exist. Also if I switch the class around so that Class2 is defined first it works.

However I was wondering if there is another way to get around this. Maybe I can somehow tell python that Class2 is going to exist so don’t worry about it, or do I just have to put them in the right order?

  • 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-06-12T00:41:02+00:00Added an answer on June 12, 2026 at 12:41 am

    The compiler doesn’t do anything here. In both cases, exactly the same bytecode sequence is generated to look up the class at runtime and instanciate it.

    What makes the difference is when the statements are run. All code in a Python module is executed top from bottom — there is no such thing as a declaration, everything’s a definition and every binding is dynamic. Code in a class definition is run when the class definition is encountered (and therefore before the second class is brought into existence and bound to the name Class2). Code in a function runs when the function is called, and because you don’t call the function before the definition of the second class, it’s available by the time you call that function.

    That’s basically what every solution boils down to: Delay binding until whatever you’re binding to exists.

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

Sidebar

Related Questions

Possible Duplicate: Two css files defining same class The answers to this question and
There are two base classes have same function name. I want to inherit both
So I have one main file, and two classes: A display Class, and a
I have two classes, one for defining the algorithm parameters and another to implement
Given the following two ways of defining a class method in Ruby: class Foo
In my system I have two different projects, one defining its interfaces and another
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have two Classes, A and B. The A class is defined as
I'm trying to learn proper way of defining classes. I have two classes where
Given two classes: class A { private $prop1; } class B extends A {

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.