473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2363
In <ma************ *************** ************@py thon.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('Detroi t', '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
3432
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 be "read". Click on "OK" to exit program. Why this error, today and not before today?
1
3796
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 after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
1
2979
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 fullpath = " http://10.60.93.51/TempFile "; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(fullpath); //req.KeepAlive = false; HttpWebResponse res; StreamReader sr; string strResult = "";
6
1147
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
1141
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 Korean government plans to select a city and a university late next
7
1686
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 to_digit(int ch) > { > return isdigit((unsigned char)ch) ? ch - '0' : -1;
1
1620
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
2197
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
55635
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.