Newbie question about nested lists and constructor functions...

Norm Lyons norm at cybersurf.net
Mon Mar 5 05:18:29 CET 2001


Sorry about that Chris.

I am trying to create instantiated objects with the following properties:

list_var = [(key, value), left, right]

where the "key, value" pair are a tuple, and the variables "left" and
"right" are empty lists [ ] which will be manipulated to hold values later
in the program after they have already been initialized to empty lists.  The
purpose of this object is to hold the necessary data for nodes in a binary
search tree.  I know how to implement this type of data structure in C++ but
I seem to be getting hung up on the syntax differences in the Python class
and member variable declarations.

An example of one of my attempts would be:

class Node:
     def __init__(self, key = ''):
          left = []
          right = []
          data = (key, 'value')
          newnode = [data, left, right]
     def info(self):
          return self.data

The "info" function is only intended to return the values of the variables
"key and value".  The other values are used as pointers to child nodes in
the tree.  As I mentioned earlier, I have been through the online tutorial a
few times and have not found any information that clears this issue up for
me.  Obviously, the code above is incomplete as it should also contain the
appropriate "if" and "elif" statements to properly branch according to the
information passed to the constructor function.  Unfortunately, I do not
know what I am doing wrong.  Any suggestions?

Norm.

P.S. Sorry about the contact information on the last message.  Someone was
fooling around with my newsgroup account settings and I didn't notice it
until the last message was already posted.




Chris Gonnerman wrote in message ...
>Could you please post example(s) so we know exactly what is giving you
>trouble?  I at least don't quite know what you are asking.
>
>The constructor function (Python calls it "__init__") is created thus:
>
>class Spam:
>    def __init__(self):
>        ...
>
>and of course __init__ might have additional arguments.  If you are
creating
>a nested list with known initializers, you might say:
>
>myvar = [ [ "list", 2, 3, 4 ], [ 'A', 'B', 'C' ] ]
>
>so if you want to create a nested list in an __init__ function:
>
>class Spam:
>    def __init__(self):
>        self.eggbasket = [ [ "list", 2, 3, 4 ], [ 'A', 'B', 'C' ] ]
>
>to assign a nested list to a class attribute.  If you are passing in
>information
>which you wish to assign that way, you would embed it in the given syntax.
>For
>instance:
>
>class Spam:
>    def __init__(self, eggs):
>        self.eggbasket = [ eggs, [ "spam", "spam", "spam" ] ]
>
>my_spam = Spam([ "egg", "egg", "egg"])
>
>which would nest the given list of eggs within the eggbasket member of
>my_spam.
>You might alternately want to make a copy of the initializer instead of
>storing
>a reference:
>
>class Spam:
>    def __init__(self, eggs):
>        self.eggbasket = [ eggs[:], [ "spam", "spam", "spam" ] ]
>
>[:] (slice of all) is the Pythonic "shallow" list copier.
>
>Of course this is all potentially nonsense as, again, I don't exactly know
>what
>you are wanting to do.
>
>----- Original Message -----
>From: "Bartok" <JollyRoger at hotmail.com>
>Subject: Newbie question about nested lists and constructor functions...
>> I need some help with nested lists when using class constructors to
>> initialize their contents.  I found the tutorial to be of not much help
in
>> this area and have tried everything that I can think of.
>>
>> I understand the process as I am an experienced C++ programmer but I seem
>to
>> be having some difficulty using the correct syntax.  Can anyone please
>help?
>
>
>





More information about the Python-list mailing list