class JustProperty(object):
def __init__(self, username, _id, xp, name):
self.username = username
self._id = _id
self.xp = xp ## private attribute
self.name = name
pro1 = JustProperty("shlee", "frhyme", 150, "seunghoonlee")
print(pro1.username)
print(pro1._id)
print(getattr(pro1, "_id"))
print(getattr(pro1, "aaa")) #error because attribute name 'aaa' doesn't exist in the object
shlee
frhyme
frhyme
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-eabf6bf709f8> in <module>()
10 print(pro1._id)
11 print(getattr(pro1, "_id"))
---> 12 print(getattr(pro1, "aaa"))
AttributeError: 'JustProperty' object has no attribute 'aaa'
class JustProperty(object):
def __init__(self, username, _id, xp, name):
self.username = username
self._id = _id
self.xp = xp ## private attribute
self.name = name
def __getattr__(self, name):
# getattr function is built-in function in python
return "{} attribute is not defined".format(name)
pro1 = JustProperty("shlee", "frhyme", 150, "seunghoonlee")
print(pro1.username)
print(pro1._id)
print(getattr(pro1, "_id"))
print(getattr(pro1, "aaa")) #error because attribute name 'aaa' doesn't exist in the object
shlee
frhyme
frhyme
aaa attribute is not defined
댓글남기기