FAQ
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?

Thanks.

Norm

Search Discussions

  • Chris Gonnerman at Mar 5, 2001 at 4:01 am
    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?
  • Norm Lyons at Mar 5, 2001 at 4:18 am
    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?

  • Chris Gonnerman at Mar 5, 2001 at 5:24 am
    ----- Original Message -----
    From: "Norm Lyons" <norm at cybersurf.net>
    Sent: Sunday, March 04, 2001 10:18 PM
    Subject: Re: Newbie question about nested lists and constructor functions...

    Sorry about that Chris.
    S'awright.
    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
    OKAY. Now we are getting somewhere. This seems to be a common mistake for
    users of other languages. Your code likely should be like this:

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

    SPECIFICALLY, you need to assign your list to a data attribute of self. I
    would write
    your code more like this:

    class Node:
    def __init__(self, key = ''):
    self.left = []
    self.right = []
    self.data = (key, 'value')
    def info(self): # not really needed with this version IMHO
    return self.data

    or perhaps:

    class Node:
    def __init__(self, key = '', value = ''):
    self.left = None # these get object references (later)
    self.right = None # so I don't think they need to be lists.
    self.key = key
    self.value = value # is this a default?
    def info(self): # not really needed with this version IMHO
    return (self.key, self.value)

    but that is a personal thing.
    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?
    I think I have this all right, but I haven't test run it yet. Good luck :-)
  • Terry Reedy at Mar 5, 2001 at 5:04 am
    "Bartok" <JollyRoger at hotmail.com> wrote in message
    news:W1Do6.4683$ke1.121750 at jekyl.ab.tac.net...
    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.
    Constructing a list, nested or not, within __init__() is no different from
    doing so anywhere else.
    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?

    It would be much easier to do so if you post both a specific example of
    what you want to do and what you have tried.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 5, '01 at 2:42a
activeMar 5, '01 at 5:24a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase