Reputation: 7609
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.
Upvotes: 0
Views: 609
Reputation: 11906
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.
Upvotes: 1