![]() Let's be even more sneaky with our dear friend and challenge its definition of "writable". > A._dict_Ī.baz is only an abstraction layer calling A.baz.fget(a) behind the scenes. The explanation, again, is that baz is an attribute of the class A, not the instance a. So, is baz a writable attribute? What about _baz? > a._dict_įrom _dict_'s perspective, _baz is a writable attribute, but baz isn't. > a.baz = 3Īlright, now we defined a.baz. ![]() ![]() We can assume that we can't find it in a._dict_ because we didn't give it a value yet. What happened here is that we deleted the instance attribute, and calling a.foo falls back again to A.foo. Note that we can keep playing with it by deleting a.foo for instance: > del a.foo Now what happens if we define it explicitly as an attribute of a? > a.foo = 1įrom our point of view, nothing really changed, a.foo is still equal to 1, but now it shows up in _dict_. Indeed, although accessible with a.foo, it is an attribute of the class A, not of the instance a. Given the above class A and knowing _dict_'s definition, can you guess what would be the value of a._dict_? Let's run a few examples showing how confusing and inaccurate it can be. Indeed, when you read this definition, can you tell what is a "writable" attribute and what it isn't? ![]() This definition is however a bit fuzzy, which leads to a lot of wrong usage of _dict_. Python documentation defines _dict_ as:Ī dictionary or other mapping object used to store an object’s (writable) attributes. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |