471,623 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,623 software developers and data experts.

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

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!
Dec 19 '06 #1
1 2181
In <ma***************************************@python. org>, 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
Dec 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ucasesoftware | last post: by
reply views Thread by John Bailo | last post: by
7 posts views Thread by spinoza1111 | last post: by
1 post views Thread by guest | last post: by
10 posts views Thread by mdh | last post: by
21 posts views Thread by phpCodeHead | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.