Issue calling method outside class python
<p>I'm working on a python assingment involving creating and using a class with several methods. I won't post the whole thing as it'll be simpler to point out my issue if I instead show an example of what I'm trying to do.</p>

<pre><code>class Fruit(self,color,name):
    def __init__(self, color, name):
        self.color = color
        self.name = name

    def return_list(self, index):
        print fruit_list[index]

fruit_list = []

fruit_1 = Fruit("red", "apple")
fruit_list.append(fruit_1.color) 
fruit_list.append(fruit_1.name)
</code></pre>

<p>So the above code works. My issue is getting return_list to work when used outside of the class, as so:</p>

<pre><code>fruit_choice = int(raw_input("What fruit would you like?"))
Fruit.return_list(fruit_choice)
</code></pre>

<p>Basically I'm trying to create a method that when called, outputs the item at an index within a list, the index being specified by user input (i.e fruit_choice = 0, printed item is first element of list.)</p>

<p>I have a basic understanding of classes, but getting a somewhat vague method to work like return_list is a little unintuitive. The error message I get:</p>

<pre><code>Fruit.return_list(fruit_choice)
TypeError: unbound method return_list() must be called with Fruit instance as first argument (got int instance instead)
</code></pre>

<p>I know that if I got the code to work the output would be "red" if input was 0 instead of "red,"apple", but that's an issue for another time.</p>

<p>If someone could point out to me what I'm doing wrong in creating/calling the return_list method, please and thanks.</p>

<p>Here's a possible solution to your problem:</p>

<pre><code>class Fruit():

    def __init__(self, color, name):
        self.color = color
        self.name = name

    def __str__(self):
        return "Color: {0} Name: {1}".format(self.color, self.name)


class FruitBasket():

    def __init__(self):
        self.fruits = []

    def get_fruit(self, index):
        try:
            value = self.fruits[index]
            return value
        except Exception as e:
            print "Error with index {0} -&gt; {1}".format(index, e)

if __name__ == "__main__":
    basket = FruitBasket()
    names = ["apple", "orange", "lemon"]
    colors = ["green", "orange", "yellow"]

    for name, color in zip(names, colors):
        fruit = Fruit(color, name)
        basket.fruits.append(fruit)

    print basket.get_fruit(0)
    print basket.get_fruit(1)
    print basket.get_fruit(2)
    basket.get_fruit(3)
</code></pre>

<p>Few comments about the above code:</p>

<ul>
<li>Fruit object doesn't have more responsabilities than setting up a fruit with a certain name &amp; color, that's pretty much. One fruit shouldn't contain any list of fruits, that should be the responsability of another object</li>
<li>FruitBasket is the container of fruits, you could do perfectly this without using OOP but for the sake of this question, this is a valid option to hold many fruits</li>
</ul>

<p>With respect to your error, the other answers and comments are already pointing out what's wrong. Also, about <code>class Fruit(self,color,name):</code>, don't do that, in that sentence you're declaring the class type, not the constructor.</p>

<p>I'd strongly recommend you try to follow some of the many <a href="https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/" rel="nofollow">OOP python tutorials</a> out there.</p>

python list python-2.7 class methods