473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2330
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Askari | last post by:
Yesterday, ALL code in python work and nothing when I close(finish) a code. Today, when I close, some raise this windows error : Instruction at "0x00FC3D70" use memory address "0x00000000". Can't...
1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
1
by: Angel | last post by:
I'm trying to connect to a fixed IP address (eg. http://10.60.903.50/TempFile) in order to retrieve one accii line of text in TempFile. I try to read the information with this code: string...
6
by: ucasesoftware | last post by:
If u have a class person, do u add a property adress, phoneNumber in this class or is it better to add for exemple a class home with in adress, phoneNumber, etc...
0
by: John Bailo | last post by:
Order me up a plate of Kim Chi: http://times.hankooki.com/lpage/tech/200602/kt2006021517494311780.htm Korea Plans to Build Linux City, University By Kim Tae-gyu Staff Reporter "The...
7
by: spinoza1111 | last post by:
"segfault city" was an interchange wherein Richard Heathfield posted a complete lie: <heathfield> Richard Heathfield wrote: > This would be a straightforward test for numerics: > > int...
1
by: guest | last post by:
Hi, I got a script from designer, there is one line dto=new window what this line do? person is a funstion template Thanks
10
by: mdh | last post by:
Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; printf("\n\n\nThe value of x is %d\n", x);
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.