Title: reusing objects instantiated from database
Tags: python
Question: Sorry if this is too simple but I'm new to databases.
Say that I have a data base with two tables, one for persons and one for countries. Each person has a country associated with them.
I wan to load the items in these tables into python objects in a lazy way. Right now I'm looping through all the rows in 'persons' and instantiating objects for them. So for each 'person' I get an object, now if I want to access the country associate with a person, I get the country_id and instantiate a brand new 'country' instance (even if all persons have the same country). For example if person1.country is XXX and person2.country is XXX the object ids are different

```class Person(object):
    def __init__(self, id):
         self.id = id

    def country():
         ...
         country_id = (gets id from db here)
         self.country = Country(country_id)
```

so I'll get

```id(person1.country) -&gt; 123
id(person2.country) -&gt; 456
```

What is the most pythonian way to have both persons share the same country instance?

Thanks for the help

Comment: Are your `Person` instances also stored in the database? You really want to use a ORM to handle your database -> object mapping, like SQLAlchemy. Don't do this manually.


Here is another answer: you can have a many to one relationship where many countries are associated with on person. person will have a country_id as foreign key constraint to the associated country id. you basically have to do a lookup for the associated country and assign the correct id from country. For a country table you would probably want to pre-fill the table with all the known countries.

the constraint prevents you from having unmatched id's in the case where you decided to delete an entry in the country table. You can define other properties such as how entries will be deleted if a country is removed from the table or updated. i.e you can have the country_id set to null in the people table or delete all the people associated with that country. look up foreign key constraints and you should find more material about the various constraint properties. you can make it illegal to delete a country if there is still a person associated with the entry. 

```CREATE TABLE Person (
  id BIGINT NOT NULL,
  country_id BIGINT NOT NULL,
  etc ...,
  PRIMARY KEY(id),
  FOREIGN KEY(country_id) REFRENCES Country(id)
)

CREATE TABLE Country(
 id BIGINT NOT NULL,
 etc..,
)
```


Here is another answer: You could implement ```Country``` as a singleton pattern.

Although I have to ask -- why does it matter?  As long as ```person1.country == person2.country```, why do you care if the ids are the same?

Comment for this answer: Then it sounds like singleton might be a good solution for you.
Comment for this answer: Thanks for the feedback, I have to read on singletons but to answer your question I do care because I have many persons, and each country is doing expensive IO operations. I can cache the operations for one country, but that doesn't help if the next country is a different instance.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:09/16/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python>', 'Title': 'reusing objects instantiated from database', 'ViewCount': 84, 'id': 52359124, 'user_AboutMe': None, 'user_AccountId': 7369503.0, 'user_DisplayName': 'Pablo Jadzinsky', 'user_Reputation': 1116.0, 'user_Views': 32.0, 'user_id': 5610162.0}}