Home > OS >  When a method has no return statement, how do I print out it's return value?
When a method has no return statement, how do I print out it's return value?

Time:03-24

I am learning about Linked Lists. For each process applied by a Method, I have a string representation. So, adding, removing, searching (i.e, displaying the result of a search), are all streamed to stdout, but I cannot seem to do this for the insertion Method.

Some Methods have a return statement, while others rely on the __repr__() for conversion to string. The insertion Method (not mine, but a course worked example) takes two arguments and does not have a return statement. The most consistent error message I get when attempting to print is TypeError: %d format: a real number is required, not NoneType, or TypeError: not enough arguments for format string, where I have replaced %d with %s.

What I do not understand is, where the same data-type is being used with all other Methods (except the argument, 'index', which is in essence, a variable for counting), the insert() Method has a problem outputting data.

The code,

Python

class Node:
  data = None
  next_node = None

  def __init__(self, data)
    self.data = data

class LinkedList:
  def __init__(self):
    self.head = None

  def is_empty(self):
    self.head = None
    
  def insert(self, data, index):
    if index == 0:
      self.add(data)

    if index > 0:
      new_data = Node(data)

    position = index
    current = self.head

    while position > 1:
      current = current.next_node
      position -= 1

    past_node = current
    future_node = current.next_node

    past_node.next_node = new_data
    new_data = current.next_node

The attempted output,

l = LinkedList()
between = l.insert(4, 3)
print("Inserted %s at index %s" % between)

Other variants of the print format have been tried f"{}", .format() and even an attempt at conversion to string str() was attempted, with no luck. Could someone explain exactly what the problem is (though, I believe it to be a NoneType issue) and how to resolve it? Thank you.

CodePudding user response:

I think you're confusing 2 things here. The value after the return statement is what the function call is replaced with when the function is called. So for example:

def square(x):
    return x*x

square(4) 

here the square(4) would be replaced with 4*4. And if you don't explicitly use a return statement than a None is returned after the last command in the function/method.

Whereas repr() is a way to specifiy the string representation of that object. So for example:

class A:
    pass

a = A()
print(a) 

might create a cryptic output of <main.A at 0x7fbc841c9490>. So if you want it to be more descriptive you could add a repr() method:

class Point:
    def __init__(self, x,y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f"Coordinates of the point are x: {self.x}, y: {self.y}"

p = Point(2,4)

And instead of the cryptic default message you'd get:

Coordinates of the point are x: 2, y: 4

So the representation is how the obj is converted to a string whereas the return value is what the function call is replaced with.

print is TypeError: %d format: a real number is required, not NoneType, or TypeError: not enough arguments for format string, where I have replaced %d with %s.

So this creates errors because %d and %s expect numbers and strings when the return type of a method without return is None.

CodePudding user response:

insert does not "have a problem" outputting data - just like standard Python lists, it is an in-place operation. You are modifying the list on which it is applied.

insert() does not need to return anything, as all the information you need is provided by you when calling it - you need to pass a list, you need to pass data to insert and you need to pass an index at which the element is to be placed - there is no new information to be gained from returning anything.

Related question: Why don't list operations return the resulting list?

  • Related