title: File structure for organizing hierarchical classes in python
body: <p>I'm trying to organize some hierarchical classes in python with an eye to reusing the subclasses elsewhere---but still with one canonical representation.  I understand this is part of what subclassing and inheritance are for, but I'm getting tripped up in actual file and directory organization as much as anything.</p>
  
  <p>Here's my case.  I'd like to define three entities:</p>
  
  <ol>
  <li>People</li>
  <li>Organizations</li>
  <li>Addresses</li>
  </ol>
  
  <p>Both people and organizations can have addresses, but addresses are always the same, whether they belong to people or organizations.  Therefore, I'd like the same validation logic (eg, "is that a valid zip code?") to be located in a single Address class stored in a single place in my project, and for People and Organizations to draw from that class.  That is, People and Organizations contain Addresses.</p>
  
  <p>I think I will be instantiating them from JSON documents.</p>
  
  <pre><code>@ensure("zip", is_valid_zip)
  class Address:
     def __init__(self,s):
        self.street = s.get('street')
        self.city = s.get('city')
        self.state = s.get('state')
        self.zip = s.get('zip')
  
  &gt;&gt;&gt; addr = Address({"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"})
  </code></pre>
  
  <p>Now, when I create, say, a person, I'd like to include the address.  If the person looks like this, and the string that creates it looks like what follows:</p>
  
  <pre><code>class Person:
     def __init__(self,s):
        self.fname = s.get('fname')
        self.lname = s.get('lname')
        self.address = Address(s.get('address'))
  &gt;&gt;&gt; bill = Person({'fname': "Billbert", "lname": "Bronson", "address": {"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"}})
  
  &gt;&gt;&gt; bill.address.street
  &gt;&gt;&gt; '123 main st'
  </code></pre>
  
  <p>...where should I store these modules, and how?  Where is the correct place for an import in my project?  I have something like this...</p>
  
  <pre><code>schemas
  ├── schemas
  │   ├── __init__.py
  │   ├── people.py
  │   ├── addresses.py
  │   └── organizations.py
  ├── README.md
  └── setup.py
  </code></pre>
  
  <p>where <code>__init__.py</code> contains:</p>
  
  <pre><code>from schemas.addresses import Address
  from schemas.people import Person
  from schemas.organizations import Organization
  </code></pre>
  
  <p>But there's something circular here.  Does the Person class itself need an import statement for Address in it?</p>
  
  <p>Now in my new script, I would like to be able to say...</p>
  
  <pre><code> &gt;&gt;&gt; from schemas import Person
   &gt;&gt;&gt; bill = Person({'fname': "Billbert", "lname": "Bronson", "address": {"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"}})
  </code></pre>
  
  <p>Among other problems, when I try this approach, I get an error</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/home/stuff/schemas/schemas/people.py", line 5, in __init__
      self.address = Address(s.get('address'))
  NameError: global name 'Address' is not defined
  </code></pre>
  
  <p>...which makes me concerned I'm barking up the wrong inheritance tree entirely.</p>
  
  <p>Any tips on how to do this /correctly/ would be greatly appreciated---even things such as "here's an alternative to loading in from JSON" or "do a fancy abstract class factory crazy thing like so." </p>