I'm trying to get a method in my child class to set attributes from the parent class as follows:
My parent class:
class user_credentials:
def __init__(self):
self.username= ""
# -----------------------------
# Getters & setters for attributes
#------------------------------
@property
def username(self):
return self._username
@username.setter
def username(self, username):
while (username == ''):
username = input('Enter a proper User name, blank is not accepted')
self._username = username
In my child class I want to set the attributes in a method as follows:
# Manage login requests for user
class login_manager(user_credentials):
def __init__(self):
super().__init__()
self.success_value = None
self.error_message = None
def request_login(self):
super(login_manager, self).username = input("Enter your user name")
When I call the object to carry out request_login
method:
lm1 = login_manager()
lm1.request_login()
I get the following error traceback:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-a712c3363bb9> in <module>
1 lm1 = login_manager()
----> 2 lm1.request_login()
3 lm1.login_request_success()
4 lm1.print_members()
<ipython-input-8-def865430eaf> in request_login(self)
12 def request_login(self):
---> 14 super(login_manager, self).username = input("Enter your user name")
AttributeError: 'super' object has no attribute 'username'
I've been doing some research on this but I can't seem to find an example of someone trying to use setter from parent class in child class.
What am I doing wrong?
This edit works for me.
def request_login(self):
self.username = input("Enter your user name")
The super
mechanism and the property
getter and setter syntax actually really confuse me, so I can't really tell you why your code shouldn't work. But since you're inheriting from the parent class anyway, you should just be able to use its methods as long as you didn't write another version in the child class.