title: Python - global name is not defined when calling function from class __init__ method
body: <p>I am new to Python and working on a class for storing meaningful data pieces about books. I have started as follows:</p>
  
  <pre><code> class BookDisplay: 
  
       def __init__(self, _name, _isbn, _price, _picture, _link):
        self.name =    _name
        self.isbn =    _isbn
        self.price =   _price
        self.picture = _picture
        self.link =    _link
        self.xmlString = MakeXMLString(_name, _isbn, _price, _picture, _link)                    
  
      name = ""
      isbn = ""
      price = 0.0
      picture = ""  #a URL
      link = "" 
  
      xmlString = ""
  </code></pre>
  
  <p>I thought this <code>__init__</code> method would just be able to call <code>MakeXMLString</code>, which I defined in the same file (bookdisplay.py), right below the <code>BookDisplay</code> class: </p>
  
  <pre><code>  def MakeXMLString(_name, _isbn, _price, _picture, _link): #python multi-line syntax  
      xmlString = "&lt;name&gt;" + _name + "&lt;/name&gt;" \
                       + "&lt;isbn&gt;" + _isbn + "&lt;/isbn&gt;" \
                       + "&lt;price&gt;" + str(_price) + "&lt;/price&gt;" \
                       + "&lt;picture&gt;" + _picture + "&lt;/picture&gt;" \
                       + "&lt;link&gt;" + _link + "&lt;/link&gt;" 
  
      return xmlString 
  </code></pre>
  
  <p>Originally, I actually had <code>MakeXMLString</code> as a method within the class, like this:</p>
  
  <pre><code> def MakeXMLString(self):  
      self.xmlString = "&lt;name&gt;" + self.name + "&lt;/name&gt;" \
                       + "&lt;isbn&gt;" + self.isbn + "&lt;/isbn&gt;" \
                       + "&lt;price&gt;" + str(self.price) + "&lt;/price&gt;" \
                       + "&lt;picture&gt;" + self.picture + "&lt;/picture&gt;" \
                       + "&lt;link&gt;" + self.link + "&lt;/link&gt;" 
  </code></pre>
  
  <p>In that case, <code>__init__</code> contained this call: </p>
  
  <pre><code>self.xmlString = self.MakeXMLString()
  </code></pre>
  
  <p>In both cases, when trying to instantiate <code>BookDisplay</code> from another file:</p>
  
  <pre><code>from bookdisplay import BookDisplay
  ...
  ...
  thumbnails = []
  ...
  thumbnails.append(BookDisplay(titleField, "-1", float(priceField), imgField, linkField))
  </code></pre>
  
  <p>...I get the following global name error (this traceback in particular for the not-within-class function):</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "search.py", line 30, in ebaySearch
      handleDocument(doc)
    File "search.py", line 59, in handleDocument
      handleItems(items, outputFile)
    File "search.py", line 102, in handleItems
      thumbnails.append(BookDisplay(titleField, "-1", float(priceField), imgField, linkField))
    File "bookdisplay.py", line 15, in __init__
      self.xmlString = MakeXMLString(_name, _isbn, _price, _picture, _link) 
  NameError: global name 'MakeXMLString' is not defined
  </code></pre>
  
  <p>What am I missing here? From what I can tell, <code>MakeXMLString</code> is perfectly accessible to the class. </p>