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

Prog

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?

user_na

Be careful cls is typically used for classmethods for example like this:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear)

So it is better to not use it for your own class names to avoid confusion.

Comming to your question. In your first block you use a reference to the class itself. So here only static variables are visible as __init__ has not been called.

>>> print(hasattr(cls1,'A'))
True
>>> print(hasattr(cls1,'a'))
False

In the second block you use an instance of the class. Here, __init__() was executed and therefore obj.a exists.

>>> obj=cls1()
>>> print(hasattr(obj,'A'))
True
>>> print(hasattr(obj,'a'))
True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get different boolean results, when they should be the same?

Why do I get different results when using the StandardScaler in GridSearchCV?

Why do MATLAB built-in function results differ from the ones i get when i define the function myself using the same expression?

Why do I get different results when I do a manual split of test and train data as opposed to using the Python splitting function

Why do I get an error for trying to access a builtin class method attribute but not the same builtin function attribute

Why do i get different object ID's when appending same object to different list in python OOP?

Why do I get different results when I run the same code in a TextChanged event as opposed to a Button Click?

Why do I get different results when using a List than using a Tuple?

Flutter - How do I get different results using the same future?

Why do i get different results when adding char?

Why do i get different PC values when using the same data set when implementing nsprcomp

Why do I get "'list' object has no attribute 'func'" when using sympy.dsolve?

I get different (unexpected) results when selecting edges by edge attribute using igraph::E()[] and igraph::edge_attr(), why?

Why am I having different accuracy results for the same network configuration when I am using Scikit-Learn Keras model function?

Why is the calculate function giving different results for same expression when written by directly referencing a measure or its components?

Why do I get an old object when creating a new object using let and new in a function within a service?

Why do I get an "AttributeError: 'tuple' object has no attribute 'step'" error when using variables to specify the step length in np.ogrid function?

Why do i get an Attribute Error when using Neurokit?

Why do I get different results when I store results to a variable?

Why do I get different results in these 2 SQL queries? Using AVG alone vs. when using over()

Why do I get the same number when using rand/srand in a function before main?

Why do i get different output when i used the same code, same data but different laptops for vba?

Why do i get different results when i use lower() and when i not use lower()?

Why do I get "object is not an instance of declaring class" when invoking a method using reflection?

In javascript how I run a function on multiple divs with the same class to get different results

Why do I get this error AttributeError: 'str' object has no attribute 'get' where the get function is on a dictionary?

Why do I get different results when calling from different file locations?

Why do I compile the same code on the Visual Studio and Qt, but get different results?

Why do I get different results with almost the same command in docker with MySQL?