python class module __getattr__
use funation obj.val1.val2
error. python program for.
class hub:
def __getattr__(self,__name):
print(__name)
obj = hub()
obj.val1.val2
error:
val1
Traceback (most recent call last):
File "hub.py", line 6, in <module>
obj.val1.val2
AttributeError: 'NoneType' object has no attribute 'val2'
Expert Value:
val1.val2
CodePudding user response:
The attribute obj.val1 is none. When you use obj.val1.val2 it actually calls the getattr of obj.val1 which is none. Because obj.val1 is none and doesn't have a val2 attribute you receive an error.
CodePudding user response:
you are not returning anything which is equivalent to returning None and then you are trying to get another attribute from none which fails.
this code should works:
class hub:
def __getattr__(self,__name):
print(__name)
return self
obj = hub()
obj.val1.val2