0

I have the defined the following class as:

def user_kitchen(handle):
    # return a BeautifulSoup object


class User(object):

    def __init__(self, handle):
        self.handle = str(handle)
        self.soup = user_kitchen(handle)
        self.details = self.find_details()

    def find_details(self):
        value_map = {}
        for detail, attribute in details_map:
            value = (self.soup).find_all(attrs=attribute)[0].text
            value_map[detail] = value
        return value_map

When I instantiate the class User as:

me = User('torvalds')

I am getting a NameError: name 'self' is not defined

Here is the traceback:

In []: me = User('torvalds')
NameError                         Traceback (most recent call last)
<ipython-input-61-f6d334f2ee24> in <module>()
----> 1 me = User('torvalds')

/home/user.py in __init__(self, handle)
     28         value_map = {}
     29         for detail, attribute in details_map:
---> 30             value = (self.soup).find_all(attrs=attribute)[0].text
     31             value_map[detail] = value
     32         return value_map

/home/user.py in _find_details(detail)
     18 
     19 
---> 20 class User(object):
     21 
     22     def __init__(self, handle):

NameError: name 'self' is not defined

I have looked at few similar questions on SO about calling instance methods from __init__ method:

Yet I am unable to fix this.

5
  • Please post full stack trace Commented Jul 10, 2016 at 16:17
  • @masnun Added it. Thanks for the siggestion :) Commented Jul 10, 2016 at 16:23
  • The lines in stack trace are not available in your code snippet. Will it be possible to post full code? Commented Jul 10, 2016 at 16:28
  • @masnun Added them as well Commented Jul 10, 2016 at 16:36
  • In your stack trace, I see this /home/user.py in _find_details(detail) - where is the _find_details(detail) method in your code? Commented Jul 10, 2016 at 16:40

1 Answer 1

1

According to your stack trace, I see a method with the signature - _find_details(detail). Inside that method, there's a line like - value = (self.soup).find_all(attrs=attribute)[0].text.

Your method doesn't take in self as the first parameter. So it can't find self in that context. Make it _find_details(self, detail) - then it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Though your answer didn't help me solve my problem, I have accepted it due to your diligence and it is technically correct. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.