Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

python classes

Name: Anonymous 2010-01-05 23:50

OK, say I need to create an application that simulates a
family tree.  Each member may have a mom dad and kids.

I need to create a family tree class that contains a
collection of these member class objects and methods for
adding branches, removing members, etc...

I'm mainly having a hard time figuring how to join the
members if that makes sense.

I've got something like...

class member(object):
   
    def __init__(self):
       member.kid = self[0]
       member.dad = self[1]
       etc...


class familyTree(object):

    def __init___(self):
        for m in mlist:
            try:
                m = member(m)

blah blah blah


How to I make the familytree class contain a
collection of member class objects?

Name: Anonymous 2010-01-06 2:20

>>3
class member(object):
You should *always* start class names with a capital letter. This is probably the most widely accepted naming convention ever, across virtually all programming languages.

member.kid = self[0]
self is not a list. What are you doing?

for m in mlist:
Where have you defined mlist? For that matter, what is it meant to be?

try:
Why on earth would you put a try block there? Do you expect member creation to fail somehow? You don't even have a matching except block.

m = member(m)
You don't seem to understand how Python's bindings work. The loop creates a local binding m to an element in mlist. You can modify the object, and it modifies the corresponding object in mlist because it's the same object, but assigning to m resets the binding to a new object. This doesn't replace it in mlist; in your current code, the created member object is just ignored.

Your sample code has an extremely high error per line ratio. You should probably get a better understanding of Python before attempting to use its object oriented features.

Real help on /prog/? It's more likely than you think

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List