GitPushPullLegs
GitPushPullLegs

Reputation: 3

Python OOP: How do I link multiple classes objects to one object

For example:

class Person(object):
    name = ""
    age  = ""

class Address(object):
    title  = ""
    street = ""
    city   = ""
    state  = ""
    zip    = ""

Let's assume I have created two Person objects.

sally = Person()
sally.name = "Sally"
sally.age  = 22

john  = Person()
john.name = "John"
john.age  = 20

If each person had only one address I could easily just put it as an attribute to the person object. However, these people have both a work address and a home address.

sally_work = Address()
sally_work.title  = "Work"
sally_work.street = "123 Random Ave"
etc..

sally_home = Address()
sally_home.title  = "Home"
sally_home.street = "456 Foo St"
etc..

john_work = Address()
john_work.title  = "Work"
john_work.street = "7899 Work Ave"
etc..

john_home = Address()
john_home.title  = "Home"
john_home.street = "541 Character Ave"
etc..

Right now these addresses are not connected in any way to their corresponding person objects. In Django I would put person = Models.ForeignKey(Person). My question is what is the equivalent to a ForeignKey in normal Python OOP? Something that'll link each address to it's corresponding Person.

Thank you all in advance!

Upvotes: 0

Views: 1968

Answers (2)

Jayanth Koushik
Jayanth Koushik

Reputation: 9894

There are quite a few issues with the way you are doing things. First and foremost, name, age etc. should be instance attributes not class attributes. One way to define the classes would be this:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Address:
    def __init__(self, title, street, city, state, zip):
        self.title = title
        self.street = street
        self.city = city
        self.state = state
        self.zip

You could actually not have fixed interface for storing addresses for people and just use different names for different 'people' like this:

sally = Person('Sally', 22)
sally.work_address = Address('Work', ...)
sally.home_address = Address('Home', ...)

Another thing to note is that you don't actually need a class to store a bunch of attributes. You can use a namedtuple.

from collections import namedtuple

Person = namedtuple('Person', ('name', 'age'))
Address = namedtuple('Address', ('title', 'street', 'city', 'state', 'zip'))

Upvotes: 1

merlin2011
merlin2011

Reputation: 75565

To answer your specific question, you probably want an list of addresses for each Person, you could then add the addresses to the Person object.

class Person(object):
    name = ""
    age  = ""
    addresses = []

However, the way you have declared this object will cause all instances of Person to share the same name, age, etc.

You probably meant to write the following, and the equivalent for class Address.

class Person(object):
    def __init__(self):
       self.name = ""
       self.age  = ""
       self.addresses = []

In order to add the Address to the Person, you would then write:

sally.addresses.append(sally_work)
sally.addresses.append(sally_home)

Upvotes: 3

Related Questions