0
class Employee():
    def __init__(self,n,s):
        self.salary = s;
        self.name = n;
 
    @property
    def name(self):
        return self.name
    
    @property
    def salary(self):
        return self.salary

    @salary.setter
    def salary(self,val):
        self.salary =val

    @name.setter
    def name(self,val):
        self.name =val
    
e=Employee("Sachin",500)

print(e.salary)
print(e.name)

ERROR: ..... line 15, in salary return self.salary [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

*Problem gets fixed when the indent everything under the constructor,but I don't understand how and why?! *

lucifer
  • 11
  • 3
  • 1
    "*Problem gets fixed when the indent everything under the constructor,but I don't understand how and why?! *" Because then there aren't any properties, they just get assigned to local variables and thrown away when `__init__` stops executing if you indent under the `__init__` – juanpa.arrivillaga Sep 13 '22 at 17:24
  • 1
    In any case, you are getting recursion because doing `self.salary =val` in the setter **calls the setter again**, which will do call itself *again*, because you have `self.salary =val` etc etc – juanpa.arrivillaga Sep 13 '22 at 17:24
  • Perhaps more importantly, what you've shown shouldn't even have `property` to begin with, these should just be regular attributes since your getters/setters *don't do anything* – juanpa.arrivillaga Sep 13 '22 at 17:25
  • @juanpa.arrivillaga Appreciate it man!! thanks. Also, the problem gets fixed if i replace self.salary/self.name with self._salary/self._name.. someone have given this explanation ***Use self._x instead of self.x for private member. By naming both member and property x property shadows member, and return self.x in getter body calls itself*** can you explain this one to me, I didn't get it – lucifer Sep 13 '22 at 17:36

0 Answers0