There has been a misunderstanding when testing the properties of an instance.
class Test(): name = ‘python‘ def printest(): print ‘Test‘a = Test()print dir(a)print a.__dict__
where Dir (a) prints the content: ['doc', 'module', ' name ', ' Printest ']
Where A. dict print out: {}
Previously mistaken for Dir (a) as an existing attribute of instance a, the actual dir means: it returns a list that contains all the names of the properties that can be found, that is, the properties of the returned class and its subclasses, and a list of methods. For example, the Class A is Test,name is actually a class attribute.
The implication of Dict is that this property is to display the properties and values in the object in a dictionary. Note that this is the object, where the object is a, and instance a itself does not have any properties, so it is called {}. (Can use A.name access is due to the reason for looking up)
For example, to set a property for a, then see the printing of two functions.
class Test(): name = ‘python‘ def __init__(self): self.lastname = ‘tttt‘ def printest(): print ‘Test‘a = Test()a.firstname = ‘hhh‘print dir(a)print a.__dict__
The printout is this:
['doc', 'init', 'module', ' FirstName ', ' LastName ', ' name ', ' Printest ']
{' LastName ': ' Tttt ', ' FirstName ': ' HHH '}
You can see that you have an instance property at this point.
How to display the "Python" Instance properties-dir, __dict__