class People:
#parrent to both Employee and Costumer classes
def __init__(self, id, name, ssn): #define class People
#self used to accesses attributes and methodes in class
#set insatance variable
self.__id = id
self.__name = name
self.__ssn = ssn
class Employee(People):
#Child of Class People
def __init__(self, id, name, ssn, password):
People.__init__(self, id, name,ssn)
self.__password = password
def __str__(self):
return f"ID :{self.__id}\nName :{self.__name}\nSSN :{self.__ssn}\nPassword :{self.__password}"
Naming variable with double underscore prefix (eg __somename
) triggers python "name mangling". This is intended to prevent name collision, and as a result, Python requires a special attribute name to access: _ClassName__somename
instead of __somename
.
For example:
class Person():
def __init__(self, name):
self.__name = name
>>> Person("x").__name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__name'
>>> Person("x")._Person__name
'x'
To fix this you should either use the "mangled" name to access, or rename your variables to remove the double underscore.
For more details see this detailed stackoverflow post on this topic .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.