Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How would I create an class with a "Person.Address.City" property?

Reply
Thread Tools

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

 
 
Jamie J. Begin
Guest
Posts: n/a
 
      12-19-2006
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!
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      12-19-2006
In <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments