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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:08:15+00:00 2026-05-10T16:08:15+00:00

Ok I have two modules, each containing a class, the problem is their classes

  • 0

Ok I have two modules, each containing a class, the problem is their classes reference each other.

Lets say for example I had a room module and a person module containing CRoom and CPerson.

The CRoom class contains infomation about the room, and a CPerson list of every one in the room.

The CPerson class however sometimes needs to use the CRoom class for the room its in, for example to find the door, or too see who else is in the room.

The problem is with the two modules importing each other I just get an import error on which ever is being imported second 🙁

In c++ I could solve this by only including the headers, and since in both cases the classes just have pointers to the other class, a forward declaration would suffice for the header eg:

class CPerson;//forward declare class CRoom {     std::set<CPerson*> People;     ... 

Is there anyway to do this in python, other than placing both classes in the same module or something like that?

edit: added python example showing problem using above classes

error:

Traceback (most recent call last):
File ‘C:\Projects\python\test\main.py’, line 1, in
from room import CRoom
File ‘C:\Projects\python\test\room.py’, line 1, in
from person import CPerson
File ‘C:\Projects\python\test\person.py’, line 1, in
from room import CRoom
ImportError: cannot import name CRoom
room.py

from person import CPerson  class CRoom:     def __init__(Self):         Self.People = {}         Self.NextId = 0      def AddPerson(Self, FirstName, SecondName, Gender):         Id = Self.NextId         Self.NextId += 1#          Person = CPerson(FirstName,SecondName,Gender,Id)         Self.People[Id] = Person         return Person      def FindDoorAndLeave(Self, PersonId):         del Self.People[PeopleId] 

person.py

from room import CRoom  class CPerson:     def __init__(Self, Room, FirstName, SecondName, Gender, Id):         Self.Room = Room         Self.FirstName = FirstName         Self.SecondName = SecondName         Self.Gender = Gender         Self.Id = Id      def Leave(Self):         Self.Room.FindDoorAndLeave(Self.Id) 
  • 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. 2026-05-10T16:08:16+00:00Added an answer on May 10, 2026 at 4:08 pm

    No need to import CRoom

    You don’t use CRoom in person.py, so don’t import it. Due to dynamic binding, Python doesn’t need to ‘see all class definitions at compile time’.

    If you actually do use CRoom in person.py, then change from room import CRoom to import room and use module-qualified form room.CRoom. See Effbot’s Circular Imports for details.

    Sidenote: you probably have an error in Self.NextId += 1 line. It increments NextId of instance, not NextId of class. To increment class’s counter use CRoom.NextId += 1 or Self.__class__.NextId += 1.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could add an ItemContainerStyle to stretch the DataTemplate to… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer While the screen is touched, there will be touch events.… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer Change your JavaScript to this <script language="javascript" type="text/javascript"> function rdBtn_onClick()… May 11, 2026 at 11:50 pm

Related Questions

I have a bunch of keys that each have an unlikeliness variable. I want
How do you get the logical xor of two variables in Python? For example,
Ok, I have two tables that I am joining and doing a select on
Ok, i have simple scenario: have two pages: login and welcome pages. im using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.