Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to Python and working on a class for storing meaningful data pieces about books. I have started as follows:

 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 = ""

I thought this __init__ method would just be able to call MakeXMLString, which I defined in the same file (bookdisplay.py), right below the BookDisplay class:

  def MakeXMLString(_name, _isbn, _price, _picture, _link): #python multi-line syntax  
    xmlString = "<name>" + _name + "</name>" \
                     + "<isbn>" + _isbn + "</isbn>" \
                     + "<price>" + str(_price) + "</price>" \
                     + "<picture>" + _picture + "</picture>" \
                     + "<link>" + _link + "</link>" 

    return xmlString 

Originally, I actually had MakeXMLString as a method within the class, like this:

 def MakeXMLString(self):  
    self.xmlString = "<name>" + self.name + "</name>" \
                     + "<isbn>" + self.isbn + "</isbn>" \
                     + "<price>" + str(self.price) + "</price>" \
                     + "<picture>" + self.picture + "</picture>" \
                     + "<link>" + self.link + "</link>" 

In that case, __init__ contained this call:

self.xmlString = self.MakeXMLString()

In both cases, when trying to instantiate BookDisplay from another file:

from bookdisplay import BookDisplay
...
...
thumbnails = []
...
thumbnails.append(BookDisplay(titleField, "-1", float(priceField), imgField, linkField))

...I get the following global name error (this traceback in particular for the not-within-class function):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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

What am I missing here? From what I can tell, MakeXMLString is perfectly accessible to the class.

share|improve this question

1 Answer

when you defined MakeXMLString as a method, it is not returning anything so

self.xmlString = self.MakeXMLString()

will overwrite self.xmlString and make it point to the method itself.

With the way you have it defined now, MakeXMLString is not accessible from the other file, so you have to manually import it as well by doing:

from bookdisplay import BookDisplay, MakeXMLString

EDIT:

upon rereading, you aren't calling MakeXMLString from the other file, so the error is in bookDisplay.py; make sure

def MakeXMLString()

is on the same indent level as the class definition, otherwise it'll be interpreted as a method.

share|improve this answer
 
Strangely enough, I made NO changes from the first approach I described (outside of class method), exited the interpreter, and tried running everything again. It worked somehow. I have no idea why. –  nicole Apr 30 at 23:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.