narancs
narancs

Reputation: 5294

BeautifulSoup and Python getter

I`m a Python noob, only started 3 days ago, so please help me: I have the following code:

def parseWebContentAndUploadDAO(self,liSet): if not liSet: print "no element found" else: print len(liSet)

    almaProdList = deque() ;

    for item in liSet:

        loopProd = Alma.Alma();
        loopProd.id=item['id']
        loopProd.id=item['data-keyword']

        detailsUrlLink=  item.find('a', attrs={'class':'item gaProductDetailsLink'})['href']
        len(detailsUrlLink)
        print detailsUrlLink
        loopProd.detailsSiteURL  = detailsUrlLink

and when I run, I see that the loopProd setter is getting called forever, so I yes I`m getting an RuntimeError: maximum recursion depth exceeded error. Please tell me, what should be the problem, and why is this code call my setter again and again until I get exception.

Please see that setter is in my custom class:

@detailsSiteURL.setter
def detailsSiteURL(self,v):
    self.detailsSiteURL = v

Upvotes: 0

Views: 144

Answers (1)

Jon Clements
Jon Clements

Reputation: 142146

You're not supposed to try and set your setter (your setter function and variable need different names):

@detailsSiteURL.setter
def detailsSiteURL(self,v):
    self.detailsSiteURL = v # <---- same name as function!
         ^^^^^^^^^^^^^^

For properties you normally follow this pattern (note the getter/setter methods are named foo and the actual variable is called _foo):

class Something(object):
  def __init__(self, foo):
      self._foo = foo
  @property
  def foo(self):
      return self._foo
  @foo.setter
  def foo(self, value):
      self._foo = value

Upvotes: 1

Related Questions