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>You seem to have a misunderstanding of Classes and class instances.</p>

<p>The <code>return_list</code> function you have defined is part of the <code>Fruit</code> class, i.e. each instance of the <code>Fruit</code> class contains this method. But the use of this function, which is to return a specific fruit from fruit list, is independent of the individual <code>Fruit</code> instances, which means this method should not have been in the <code>Fruit</code> class in the first place.</p>

<p>The signature of class definition <code>class Fruit(self,color,name)</code> is wrong. It should be <code>class Fruit:</code>. The initial parameters should be defined as parameters to the <code>__init__</code> method.</p>

<p>The list <code>fruit_list</code> should contain <code>Fruit</code> instances. The program could be written as:</p>

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

    def __str__(self):
        return 'I am a ' + self.color + ' ' + self.name

fruit_list = []

fruit_1 = Fruit("red", "apple")
fruit_list.append(fruit_1)

fruit_choice = int(raw_input("What fruit would you like?"))
print(fruit_list[fruit_choice])
</code></pre>

<p>Printing a Python object calls the <code>__str__</code> magic method.</p>

<p>If you do really want to have a separate function which returns a <code>Fruit</code> from the <code>fruit_list</code>, you can define a function outside the class (but these seem unnecessary when you can directly access the instance using the index):</p>

<pre><code>def return_list(list, index):
    return list[index]
</code></pre>

<p>or</p>

<pre><code>def return_list(index):
    global fruit_list
    return fruit_list[index]
</code></pre>

<p>If you try to print the entire list using <code>print(fruit_list)</code> the output is:</p>

<pre><code>[&lt;__main__.Fruit instance at 0x110091cf8&gt;]
</code></pre>

<p>That is because Python does not call <code>__str__</code> on each instance when you print a list of instances. You can print the <code>fruit_list</code> by doing:</p>

<pre><code>for fruit in fruit_list:
    print(fruit)
</code></pre>

python list python-2.7 class methods