class question

S

sittner

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
 
S

Scott David Daniels

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
(e-mail address removed)
 
D

Dennis Lee Bieber

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:

When is nesting classes "simple".
class father:
age=...
name=....
def abcd.....
class son(father):
age=....
name=....
def efgh:
or any other heirarchic structure of class and subclasses.

What is the difference between a son and a father? Other than an
implied relationship...

class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
self.father = None #not defined at present
self.children = [] #none defined at present

p1 = Person("jack", 24)
p2 = Person("sam", 58)

p1.father = p2
p2.children.append(p1)
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

"son" and "father" are both persons -- not a hierarchy of classes.
Your inheritance hierarchy expands to: a Son IS A Father! (Rather than:
a Son HAS A Father) A hierarchy of classes would be, maybe:

class Person(object):
...

class Male(Person):
...

class Female(Person):
...

(a Male IS A Person; a Female IS A Person) which would allow for adding
attributes specific to the subclass. Or...

class Vehicle(object):
...

class AirVehicle(Vehicle):
...

class LandVehicle(Vehicle):
...

class SeaVehicle(Vehicle):
...

class Airship(AirVehicle):
...

class Airplane(AirVehicle):
...

class TurboJet(Airplane):
...

class TurboProp(Airplane):
...

class Glider(Airplane):
...

etc.

A TurboProp IS An Airplane IS An AirVehicle IS A Vehicle. A Vehicle HAS
A Position, HAS A Velocity, HAS A Capacity, etc. All these "has"s are
common to the subclasses.
get their names and ages and use the class as a data structure for that
matter).

sp = p1
while sp:
print sp.name, sp.age
sp = sp.father

(assuming you are doing a trace from youngest to oldest in direct line;
going the other way gets trickier and recursive)

def printPerson(sp, level=0):
print " " * level, sp.name, sp.age
for c in sp.children:
printPerson(c, level+1)

printPerson(p2)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top