0

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!

1
  • 1
    @GiovanniMacciocu, His question is about how to do it outside django not inside django. Commented Apr 28, 2014 at 9:42

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response! I appreciate your input.
1

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'))

1 Comment

Thank you for your prompt response, I believe you've given me just what I'm looking for! The reason I'm not using a class is because there is some specialized behavior that will be used, so a tuple wouldn't really be convenient for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.