Suppose I have a Python class called Person
in file person.py
as follows:
class Person(object):
static_id = 0
instance_id = None
@staticmethod
def new_id():
Person.static_id += 1
return Person.static_id
def __init__(self):
self.instance_id = Person.new_id()
def __repr__(self):
return "<Person #%s>" % (self.instance_id)
Instantiating Person
works:
>>> a = Person()
>>> a
<Person #1>
>>> b = Person()
>>> b
<Person #2>
And I have another class called Thing
in file thing.py
as follows:
from person import Person
class Thing(object):
static_id = 0
instance_id = None
instance_creator = None
@staticmethod
def new_id():
Thing.static_id += 1
return Thing.static_id
def __init__(self, person):
self.instance_id = Thing.new_id()
self.instance_creator = person
def __repr__(self):
return "<Thing #%s created by %s>" % (self.instance_id, self.instance_creator)
Now, when I instantiate instances of Thing
I see the following:
>>> Thing(person=b)
<Thing #1 created by <Person #2>>
>>> Thing(person=b)
<Thing #2 created by <Person #2>>
>>> Thing(person=a)
<Thing #3 created by <Person #1>>
How can I create a (non-static) method Person.created_things()
that will return me all Things created by that instance of Person? I want:
>>> b.created_things()
[<Thing #1 created by <Person #2>>, <Thing #2 created by <Person #2>>]
But I cannot figure out how to do this without importing thing.Thing
into person.py
. Doing so would create a circular import reference. So how can I do it? In this case these are not Django classes.