A scoping question

Steven Bethard steven.bethard at gmail.com
Tue Dec 28 21:15:41 CET 2004


It's me wrote:
> This must be another newbie gotchas.
> 
> Consider the following silly code
[snip tightly coupled code]

A few options that also might work better than such tightly coupled modules:

-------------------- file1.py --------------------
import file2
myBaseClass = file2.BaseClass()
class NextClass:
     def __init__(self):
         for eachChild in myBaseClass.MyChilds:
             pass
myBaseClass.AddChild(file2.NextClass())
--------------------------------------------------

-------------------- file2.py --------------------
class BaseClass:
     def __init__(self):
         self.MyChilds = []
     def AddChild(self, NewChild):
         self.MyChilds.append(NewChild)
--------------------------------------------------


or


-------------------- file1.py --------------------
import file2
myBaseClass = file2.BaseClass()
myBaseClass.AddChild(file2.NextClass(myBaseClass))
--------------------------------------------------

-------------------- file2.py --------------------
class BaseClass:
     def __init__(self):
         self.MyChilds = []
     def AddChild(self, NewChild):
         self.MyChilds.append(NewChild)
class NextClass:
     def __init__(self, myBaseClass):
         for eachChild in myBaseClass.MyChilds:
             pass
--------------------------------------------------



More information about the Python-list mailing list