473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Python circular class inclusion

Suppose I have a class setup like this:

Expand|Select|Wrap|Line Numbers
  1. class Foo:
  2.       Bar b
  3. class Bar:
  4.       Foo f
  5.  
How can I make this work, preferably in one file?
Mar 21 '08 #1
4 1324
micmast
144 100+
I presume you mean something like this:

Expand|Select|Wrap|Line Numbers
  1. class Foo():
  2.  
  3.     def __init__(self):
  4.         b = Bar()
  5.         b.hello()
  6.  
  7. class Bar():
  8.  
  9.     def hello(self):
  10.         print "hello"
  11.  
  12. f = Foo()
  13.  
$> python test.py
hello

To others, how do you create those python code blocks? I have been trying but I cannot figure it out :s
Mar 21 '08 #2
elcron
43
I presume you mean something like this:

Expand|Select|Wrap|Line Numbers
  1. class Foo():
  2.  
  3.     def __init__(self):
  4.         b = Bar()
  5.         b.hello()
  6.  
  7. class Bar():
  8.  
  9.     def hello(self):
  10.         print "hello"
  11.  
  12. f = Foo()
  13.  
$> python test.py
hello

To others, how do you create those python code blocks? I have been trying but I cannot figure it out :s
Just start the block with [code=python]But I think he means something like
Expand|Select|Wrap|Line Numbers
  1. class Foo():
  2.     def __init__(self):
  3.         self.b = Bar()
  4.  
  5. class Bar():
  6.     def __init__(self):
  7.         self.f = Foo()
  8.  
  9. f = Foo()
  10.  
A fix would be something like
Expand|Select|Wrap|Line Numbers
  1. class Foo():
  2.     def __init__(self, bar=None):
  3.         self.b = bar
  4.         if bar == None:
  5.             self.b = Bar(self)
  6.  
  7. class Bar():
  8.     def __init__(self, foo=None):
  9.         self.f = foo
  10.         if bar == None:
  11.             self.f = Foo(self)
  12.  
  13. f = Foo()
  14.  
Mar 21 '08 #3
yes, sorry, I was thinking java when I wrote that. Thanks!
Mar 21 '08 #4
yes, sorry, I was thinking java when I wrote that. Thanks!
Mar 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
9
by: rammel | last post by:
class1.h ----------- #ifndef CLASS1_H_ #define CLASS1_H_ #include "class2.h" // inclusion class class2; // and forward class class1 { private:
3
by: joshc | last post by:
I've found some old posts on this issue but the answer was a bit vague to me. The problem I am trying to solve is the following: /***** a.h ***********/ #include "b.h" /* Forward declaration...
1
by: xoinki | last post by:
hi all, I have 3 classes. class A, class B, and class C, class D, defined in 4 files A.cpp, B.cpp, C.cpp, D.cpp inheritence hierarchy is as follows. A--> B --> C | | ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.