Why do I get different results when using ‘hasattr’ function on the class and its object for the same attribute?

When I execute this code:

class cls :
    A=1
    def __init__(self):
        self.a=2
        
obj=cls()

print(hasattr(cls,'A'))
print(hasattr(cls,'a'))

print(hasattr(obj,'A'))
print(hasattr(obj,'a'))

I get this output:

True
False
True
True

Everything is clear to me except for the second line. Why do I get False when I execute the hasattr function on the class while I got True when using it with the object for the same attribute?



Read more here: https://stackoverflow.com/questions/67941832/why-do-i-get-different-results-when-using-hasattr-function-on-the-class-and-it

Content Attribution

This content was originally published by prog at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: