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

J

Jamie J. Begin

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:
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!
 
M

Marc 'BlackJack' Rintsch

Jamie J. Begin said:
Let's say I wanted to create an object that simply outputted something
like this:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top