Reputation: 2519
I tried writing a custom getter for a class attribute. But I am unable of finding the right syntax in Python. I tried what is mentioned in this question but using the @property
decorator does not seem to work or I am calling it incorrectly.
Class:
class Comment:
def __init__(self):
self.productRecommender = None
@property
def productRecommender(self):
return self.productRecommender if self.productRecommender is not None else 'Onbekend'
Setting the attribute in the code (uses Selenium, comment is an instance of Comment()), should stay None
if class is not found:
try:
comment.recommender = comment.find_element_by_class_name('recommend').text
except NoSuchElementException:
pass
Trying to access the value
comment.productRecommender
Upvotes: 2
Views: 121
Reputation: 159
it should be self._productRecommender and not self.productRecommender
class Comment():
def __init__(self):
self._productRecommender = None
@property
def productRecommender(self):
print("getter called")
return self._productRecommender if self._productRecommender is not None else 'Onbekend'
c = Comment()
foo = c.productRecommender
Upvotes: 1