1

I am kind of new to python. My background is more in Java and C++. I'm trying to create a linked list with multiple variables. I know it can be done in Java and C++ but im not sure how to set it up with python. Now i have code that I have found on other websites that work with a single variable but i need it to hold several pieces of information. what i currently have now is:

 class Node:
     def __init__(self, cargo=None, next=None):
         self.cargo = cargo
         self.next  = next

     def __str__(self):
         return str(self.cargo)

This is OK but i need more info then this. With C++ i could put as many variables as i wanted in the struct. can someone please advise my on what i need to change about this so that i can use many pieces of data. for example: movie title, Rating, Genre, ect. (not the project just the example). The goal of the project to to compare two very very large list to each other. or maybe someone has a more efficient way of doing this. I would love to hear about it.

thanks in advance

4
  • What about your current code prevents you from using multiple pieces of data? Just create an object or dict or something containing all the info you want, and use that as the "cargo". Commented Jan 31, 2014 at 18:54
  • im just unsure on how to set it up. Commented Jan 31, 2014 at 20:12
  • Your Node class doesn't need to change in order to store many pieces of data. Are you just asking how to create a class in Python? Commented Jan 31, 2014 at 21:14
  • In this case Yes. how would I set up a class to have multiple pieces of data to use for a linked list? Commented Feb 3, 2014 at 17:34

1 Answer 1

4

Python uses something called 'duck typing'. If an object walks like a duck and quacks like a duck, then it's a duck. Think of 'walk' and 'quack' as object methods. As long as those methods exist on the object, then they can be called. So your cargo can be anything as long as all of the methods you call actually exist on it.

A Java analogy that might help is if you simply imagine Node.cargo as the base Object class, and somewhere else in your code you might explicitly cast it before calling a method. If the cast fails, then you get an exception. The difference with Python is that you don't cast before calling the method, and instead it just checks to see whether that method exists on the object (that is, does it walk like a duck).

So in this example, if we set up a bunch of nodes that support the len() function, and then iterate over the nodes, everything is fine:

node3 = Node('last')
node2 = Node(['m', 'i', 'd'], node3)
node1 = Node(('fir', 'st'), node2)

def visit_all(node):
    while node:
         print(type(node.cargo), node, len(node.cargo))
         node = node.next

>>> visit_all(node1)
 <class 'tuple'> ('fir', 'st') 2
 <class 'list'> ['m', 'i', 'd'] 3
 <class 'str'> last 4

However, if we try to pass in a node that doesn't support len(), then we get an exception, similar to a Java ClassCastException.

node0 = Node(0, node1)

>>> visit_all(node0)
 TypeError: object of type 'int' has no len()

So with all of that said, if you want to group together a movie title, Rating, Genre, etc. then you would likely store those in a separate class, and not inside Node. If your grouped data is immutable, you could also look at using a namedtuple. Again, think of cargo as a Java Object that you don't have to explicitly cast before calling its methods or properties.

EDIT: OP requested an explicit example of having Node store a custom class.

class Movie(object):
    def __init__(self, title, rating, actors):
        self.title = title
        self.director = director
        self.actors = actors

node = Node('Some Movie', 'PG', ('John Doe', 'Jane Doe'))

>>> print(node.cargo.title)
 Some Movie

Or if the Movie class can be immutable:

from collections import namedtuple

Movie = namedtuple('Movie', ['title', 'rating', 'actors'])
node = Node('Some Movie', 'PG', ('John Doe', 'Jane Doe'))

>>> print(node.cargo.rating)
 PG
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.