Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:54:46+00:00 2026-06-06T23:54:46+00:00

The following code is not returning None but some mysterious main _.link_list object: class

  • 0

The following code is not returning None but some mysterious “main_.link_list object”:

class link_list(object):
    """a link list class"""
    def __init__(self, list):
        super(link_list, self).__init__()
        pdb.set_trace()
        if list==[]:
            return None
        else:
            self.value = list[0]
            self.next = link_list(list[1:len(list)])

Test code prints ‘False’:

l=link_list([1,2,3])
print l.next.next.next==None

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:54:47+00:00Added an answer on June 6, 2026 at 11:54 pm

    .__init__() is not really a constructor.

    __init__ as a constructor?

    The object will be created whether or not you return None from .__init__(). As @Ignacio Vazquez-Abrams noted in the comments, Python expects None as the return value from .__init__(); you can’t return anything else anyway. If you need to signal some sort of catastrophic error, you should raise an exception.

    In your example, you should probably have a class that represents a complete linked list, with a “head” reference and a “tail” reference, and another class that represents a data entry in the linked list. Then the linked list class itself can be tested for a null list by checking to see if head is None.

    EDIT: Upon a little bit of further thought, your single-class implementation will work; it’s just that we need to set .next to None inside .__init__(). Here is my edit of your code:

    class link_list(object):
        """a link list class"""
        def __init__(self, lst):
            super(link_list, self).__init__()
            pdb.set_trace()
            if len(lst) > 1:
                self.value = lst[0]
                self.next = link_list(lst[1:])
                print "id == %d, linked in %d" % (id(self), id(self.next))
            elif len(lst) == 1:
                self.value = lst[0]
                self.next = None
                print "length-one case: id == %d" % id(self)
            else:
                self.value = None
                self.next = None
                print "length-zero case: id == %d" % id(self)
    
    l=link_list([1,2,3])
    print l.next.next.next is None
    

    Notes:

    • I fixed the indentation.

    • I used lst instead of list for the variable name, so as not to shadow the built-in type list.

    • I added some print statements to help you watch what is going on.

    EDIT: And, for completeness, here is an implementation using two classes: a class that represents a list and might be zero-length, and a class that represents one entry in the list. A for loop builds the chain of entries, and can build a list from any sequence (including an iterator); slicing is not used so the sequence does not have to be a list.

    class link_list_val(object):
        """one value for a linked list"""
        def __init__(self, value):
            self.value = value
            self.next = None
    
    class link_list(object):
        """a link list class"""
        def __init__(self, seq):
            self.head = None
    
            for value in seq:
                x = link_list_val(value)
                if self.head is None:
                    self.head = x;
                    cur = x
                else:
                    cur.next = x
                    cur = x
    
    l=link_list([1,2,3])
    print l.head.next.next.next is None
    

    EDIT: Here’s one more version. This uses iteration to build the linked list, not tail recursion, but does it with a single class. The trick is that the .__init__() function needs to know whether it is building a whole linked list, or just making one instance to be used to build a linked list; hence the check for seq is None. If the user passes in a sequence, .__init__() builds a whole linked list, but otherwise it just makes one instance to be used as a link in the list.

    class link_list(object):
        """a link list class"""
        def __init__(self, seq=None):
            super(link_list, self).__init__()
            if seq is None:
                return
            pdb.set_trace()
            itr = iter(seq)
            try:
                self.value = next(itr)
            except StopIteration:
                self.value = None
                self.next = None
                return
            cur = self
            for value in itr:
                cur.next = link_list()
                cur = cur.next
                cur.value = value
            cur.next = None
    
    l=link_list([])
    print l.next is None
    l=link_list([1,2,3])
    print l.next.next.next is None
    

    First we get a new iterator from the sequence. Then we attempt to pull a value from the sequence with next(). If this fails we have a zero-length linked list and return it; if this succeeds, we use that value for the head of the linked list and then loop over the rest of the values from the sequence.

    Playing directly with iterators may seem tricky, but I think it is the cleanest way to handle cases like this, where we want to pull the first item from the sequence and then loop over the remaining ones, and we don’t want to use slicing (and thus only work with an actual list).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code returning the error 'object' does not contain a constructor
The following class is not thread-safe (as proven in Proving the following code not
The following code is not working: private void fileNameLinkButton_Click(object sender, RoutedEventArgs e) { HyperlinkButton
The following test code (F#) is not returning the result I'd expect: let safeCount()
I am trying to use the following code, but it does not work when
The following code snippet is returning an error in firebug: Parameter is not an
I'm trying to save a file at path WindowsFormsApplication1\WindowsFormsApplication1\SaveFile but the following code returning
consider the following code if insert() return the list itself. def sieve(l): if not
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)
Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.