473,946 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mutually referencing imports -- impossible?

I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.

In the Car class, for some bizarre reason, I instantiated a Motorcycle.
Please pretend that this can't be avoided for now.

Meanwhile, my Motorcycle class instantiated a Car as well.

Then I moved the Car and Motorcycle classes into separate files. Each
imported the Vehicle module.

Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. The same problem happened in my Motorcycle
module. Car and Motorcycle can't both import each other.

In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.

I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.

Any ideas?

Matt


Jul 13 '08 #1
5 1026
Matthew Wilson wrote:
I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.

In the Car class, for some bizarre reason, I instantiated a Motorcycle.
Please pretend that this can't be avoided for now.

Meanwhile, my Motorcycle class instantiated a Car as well.

Then I moved the Car and Motorcycle classes into separate files. Each
imported the Vehicle module.

Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. The same problem happened in my Motorcycle
module. Car and Motorcycle can't both import each other.

In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.

I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.

Any ideas?

Matt
It is easy for imports to be mutually referencing. This presents no
problem to Python if the importing of one module is interrupted by the
import of another.

However, if one of them imports specific names from a module,
from ABC import abc
or
from ABC import *

then it's possible that the (interrupted) import of module ABC has not
progressed to the point that abc is defined.

The solution: Just
import ABC
and later reference ABC.abc

That being said, it is still a good design practice to structure your
modules hierarchically rather than a circularly.

Gary Herron
>
--
http://mail.python.org/mailman/listinfo/python-list
Jul 13 '08 #2
On Jul 13, 1:55*pm, Matthew Wilson <m...@tplus1.co mwrote:
I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.

In the Car class, *for some bizarre reason, I instantiated a Motorcycle..
Please pretend that this can't be avoided for now.

Meanwhile, my Motorcycle class instantiated a Car as well.

Then I moved the Car and Motorcycle classes into separate files. *Each
imported the Vehicle module.

Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. *The same problem happened in my Motorcycle
module. *Car and Motorcycle can't both import each other.

In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.

I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.

Any ideas?
First thing to do is ask yourself:
Are the Car and Motorcycle being created at import time, or are they
being created after the imports by a function call?

If it's the former, then yes, it's impossible. You can't do this, for
instance:

car.py:
-----------------
import motorcycle
a = motorcycle.Moto rcycle()
-----------------

motorcycle.py:
-----------------
import car
b = car.Car()
-----------------
However, you can stick them in functions and call them afterwards and
it will work:

car.py:
-----------------
import motorcycle
def create_motorcyc le():
global a
a = motorcycle.Moto rcycle()
-----------------

motorcycle.py:
-----------------
import car
def create_car():
global b
a = car.Car()
-----------------

vehicle.py
-----------------
import car
import motorcycle
car.create_moto rcycle()
motorcycle.crea te_car()
-----------------
Second, if you're using from ... import statements, it won't work; you
should change it to use plain imports as I did above. So the
following wouldn't work :
motorcycle.py:
-----------------
from car import *
a = Motorcycle()
-----------------

car.py:
-----------------
from motorcycle import *
b = Car()
-----------------

Third, if Motorcycle and Car are inside packages, you still need to
avoid from ... import even just to import the module (unless you're
willing to hook into the import machinery). For example, if car.py,
motorcycle.py, and vehicle.py are all parts of the package carsim,
then you'd have to do this:

motorcycle.py:
----------------
import carsim.car
def create_car():
global a
a = carsim.car.Car( )
----------------

and not

motorcycle.py:
----------------
from carsim import car
def create_car():
global a
a = car.Car()
----------------

This last limitation is due to a wart in the import logic.
Carl Banks
Jul 13 '08 #3
On Jul 13, 11:55*am, Matthew Wilson <m...@tplus1.co mwrote:
I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.

In the Car class, *for some bizarre reason, I instantiated a Motorcycle..
Please pretend that this can't be avoided for now.

Meanwhile, my Motorcycle class instantiated a Car as well.

Then I moved the Car and Motorcycle classes into separate files. *Each
imported the Vehicle module.

Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. *The same problem happened in my Motorcycle
module. *Car and Motorcycle can't both import each other.

In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.

I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.

Any ideas?

Matt
While it's possible for circular imports to work, it's very dangerous:
it's not always possible to tell what went wrong without tracking down
the process of the import step by step. There are more productive ways
of banging your head against the wall and going insane.

In your situation, it might be a whole lot easier to extract a common
superclass that both of your classes could inherit from.

John Roth
Jul 13 '08 #4
On Jul 14, 3:55 am, Matthew Wilson <m...@tplus1.co mwrote:
I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.

In the Car class, for some bizarre reason, I instantiated a Motorcycle.
Please pretend that this can't be avoided for now.

Meanwhile, my Motorcycle class instantiated a Car as well.

Then I moved the Car and Motorcycle classes into separate files. Each
imported the Vehicle module.

Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. The same problem happened in my Motorcycle
module. Car and Motorcycle can't both import each other.
And they should not import each other.
>
In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.
You seem to have a strange notion of "worked fine".
>
I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.
What is making a file with 3 classes "enormous"? ? What is "enormous"?
>
Any ideas?
*WRONG WAY*
*GO BACK*

Your structure is not only bizarre, it is also (sticking with only 1
letter of the alphabet) a Byzantine, baroque, and broken concept.

Asking us to "pretend that this can't be avoided for now" is asking
us to aid and abet you in creating a meaningless and unmaintainable
monster. Consider adding Truck and Bus subclasses. Will each subclass
instantiate the other 3??? You should be able to add or remove a
subclass without having to modify all other subclasses.

The only rational solution is not to have the subclasses refer to each
other. Tell us *why* you think you need to have Car refer to
Motorcycle and vice versa, and we should be able to help you find a
way out.

Cheers,
John
Jul 13 '08 #5
On Jul 14, 5:30 am, John Roth <johnro...@gmai l.comwrote:
On Jul 13, 11:55 am, Matthew Wilson <m...@tplus1.co mwrote:
I started off with a module that defined a class Vehicle, and then
subclasses Car and Motorcycle.
In the Car class, for some bizarre reason, I instantiated a Motorcycle.
Please pretend that this can't be avoided for now.
Meanwhile, my Motorcycle class instantiated a Car as well.
Then I moved the Car and Motorcycle classes into separate files. Each
imported the Vehicle module.
Then I discovered that my Car module failed because the global
Motorcycle wasn't defined. The same problem happened in my Motorcycle
module. Car and Motorcycle can't both import each other.
In the beginning, when all three (Vehicle, Car, and Motorcycle) were
defined in the same file, everything worked fine.
I don't know how to split them out in separate files now though and I
really wish I could because the single file is enormous.
Any ideas?
Matt

While it's possible for circular imports to work, it's very dangerous:
it's not always possible to tell what went wrong without tracking down
the process of the import step by step. There are more productive ways
of banging your head against the wall and going insane.

In your situation, it might be a whole lot easier to extract a common
superclass that both of your classes could inherit from.
Like the Vehicle class the OP mentioned?

Jul 13 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2207
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
4
2309
by: Ron | last post by:
I need to write a custom textbox control that references an object located in the Global.asax class, but I can't compile the control into a DLL without including the reference to the project DLL that includes the Global.asax assembly. I've tried reflection, but I can't seem to make it work correctly. Does anyone have any ideas how I can make this happen? Thanks,
4
1352
by: Mark Hageman | last post by:
Newbie question: How can a dll be referenced from the code-behind for an aspx page? I have the dll in the /bin directory, and have tried using the line "Imports " in the code-behind, but I get a compiler error message that the dll can't be found. This is a dll that I did not create, and so I'm not sure how to determine what the namespace is, if that's even what I need to do. I'm also trying to do this without pre-compiling the...
0
1544
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page codebehind, I want a member variable for each usercontrol. My problem is that I keep getting a compiler error on the usercontrol variables in my page codebehind (BC30002: Type 'CityGeoPosSelectClass' is not defined.) For whatever reason, my usercontrol class is not being declared in the page...
3
5966
by: Pat Mac | last post by:
Ok, I'm stumped. I'm working with the axWebBrowser control and all of the samples talk about adding a reference to the MSHTML library. I have the ..dll in my \windows\system32 directory, but when I'm trying to add the reference to my VB.Net application I keep getting a "namespace or type 'mshtml' for Imports 'mshtml' cannot be found." The code looks like: Imports System Imports Mshtml
2
1440
by: Keith Jakobs, MCP | last post by:
Hi All, Can anyone help me figure out how to import the 'System.DirectoryServices' namespace to a code-behind file for an ASP.NET VB form when I am NOT using VS.NET Studio??? I realize that a reference to the .dll needs to be specified in the project, and I even know which .dll needs to be referenced, but since I am only coding with a text editor at this time, using 'imports system.DirectoryServices' does not find the correct .dll. I...
2
2302
by: Can | last post by:
I have a Microsoft Access front end, SQL server backend and I am using .dll files to complete the n-tier application. How do I reference an unbound textbox (from Microsoft Access) in the .dll? In the code below it does not recognize Forms!SQL_Server_Prompt Patient_Hospital_ID -> unbound textbox in a MS Access form Select_Waiting_List -> unbound textbox in a MS Access form Select_OR_Completed -> unbound textbox in a MS Access form...
5
1290
by: PJ | last post by:
I have a class defined as so: public class Pager : Control { private PagingList<T> itemList; .... I'm getting an error message that says, "The type or namespace name 'T' could not be found. Obviously, I'm just trying to define a class that PagingList is a class that compiles just fine that inherits from List<T>.
0
1058
by: planetmatt | last post by:
I am trying to copy a text file into a sql server database but I am having problems with an undeclared variable error. The variable in question is a reference to the connection string name in the web.config file. Here is the connectionstring section of my web.config: There are 2 strings, one called ConLocText and one called ConSQLLocal ***************************************** <connectionStrings> <add name="ConLocText"...
0
10150
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9975
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10679
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6097
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3526
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.