How would I create an class with a "Person.Address.City" property?

Discussion in 'Python' started by Jamie J. Begin, Dec 19, 2006.

  1. I'm very new to the world of Python and am trying to wrap my head around
    it's OOP model. Much of my OOP experience comes from VB.Net, which is
    very different.

    Let's say I wanted to create an object that simply outputted something
    like this:

    >>> import employees
    >>> person = employee("joe") # Get Joe's employment file
    >>> print employee.Title # What does Joe do?

    Developer
    >>> print person.Address.City # Which city does Joe live in?

    Detroit
    >>> print person.Address.State # Which state?

    Michigan

    To do this would I create nested "Address" class within the "employee"
    class? Would it make more sense to just use "print
    person.Address('City')" instead?

    Thanks for your help!
     
    Jamie J. Begin, Dec 19, 2006
    #1
    1. Advertising

  2. In <>, Jamie J. Begin
    wrote:

    > Let's say I wanted to create an object that simply outputted something
    > like this:
    >
    >>>> import employees
    >>>> person = employee("joe") # Get Joe's employment file
    >>>> print employee.Title # What does Joe do?

    > Developer
    >>>> print person.Address.City # Which city does Joe live in?

    > Detroit
    >>>> print person.Address.State # Which state?

    > Michigan
    >
    > To do this would I create nested "Address" class within the "employee"
    > class? Would it make more sense to just use "print
    > person.Address('City')" instead?


    That depends on the usage of the addresses. If you need them as objects
    with "behavior" i.e. methods then you would write an `Address` class. If
    you can live with something more simple than a `dict` as `address`
    attribute of `Employee` objects might be enough.

    BTW you wouldn't create a nested `Address` *class*, but hold a reference
    to an `Address` *object* within the `Employee` *object*.

    class Address(object):
    def __init__(self, city, state):
    self.city = city
    self.state = state

    class Employee(object):
    def __init__(self, name, title, address):
    self.name = name
    self.title = title
    self.address = address

    employees = { 'Joe': Employee('Joe',
    'Developer',
    Address('Detroit', 'Michigan')) }

    def employee(name):
    return employees[name]


    def main():
    person = employee('Joe')
    print person.title
    print person.address.city
    print person.address.state

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Dec 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Stevens
    Replies:
    1
    Views:
    351
    Curt_C [MVP]
    Dec 28, 2004
  2. Geoff Winsor
    Replies:
    3
    Views:
    4,603
    Roedy Green
    Jul 7, 2004
  3. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  4. Joe P. Cool

    method to create class property

    Joe P. Cool, Mar 18, 2008, in forum: Python
    Replies:
    5
    Views:
    362
    Diez B. Roggisch
    Mar 19, 2008
  5. Spam Catcher
    Replies:
    4
    Views:
    156
    Darko
    Nov 21, 2007
Loading...

Share This Page