473,385 Members | 1,912 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,385 software developers and data experts.

class question

Hello there,
i am pretty new to object-oriented programming and i have a question:
let's say i have a simple class such as:
class father:
age=...
name=....
def abcd.....
class son(father):
age=....
name=....
def efgh:
or any other heirarchic structure of class and subclasses.
i would like to list or print the data content of a given instance of the
subclass, all the way up (e.g. if sam is jack's son, so i would like to
get their names and ages and use the class as a data structure for that
matter).
how do i do this?
thanks,
t

Mar 17 '07 #1
1 1068
si*****@lkb.ens.fr wrote:
Hello there,
i am pretty new to object-oriented programming and i have a question:
let's say i have a simple class such as:
class father:
age=...
name=....
def abcd.....
class son(father):
age=....
name=....
def efgh:
or any other heirarchic structure of class and subclasses.
i would like to list or print the data content of a given instance of the
subclass, all the way up (e.g. if sam is jack's son, so i would like to
get their names and ages and use the class as a data structure for that
matter).
You misunderstand Python's classes. There is little, if any advantage
to defining a class inside another class.

How about:
import weakref # to keep everyone from being immortal.

class Person(object):
def __init__(self, age, name, dad=None, mom=None):
self.name = name
self.age = age
self.dad = dad
self.mom = mom
self._kids = weakref.WeakValueDictionary()
if dad is not None:
dad.add_kid(self)
if mom is not None:
mom.add_kid(self)

def children(self):
return self._kids.values()

def add_kid(self, child):
assert self.age child.age
self._kids[id(child)] = child

def __repr__(self):
return '%s(%s)' % (self.name, self.age)

guy = Person(age=28, name='George')
gal = Person(age=31, name='Martha')
kid = Person(age=1, name='Ellen', dad=guy, mom=gal)
print '%s of %s and %s.' % (kid, kid.dad, kid.mom)
print "%s's kids: %s." % (guy, guy.children())
print "%s's kids: %s." % (gal, gal.children())

--Scott David Daniels
sc***********@acm.org
Mar 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
2
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
2
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
2
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.