3

I am trying to post this for hours. But it always prevented due to some code format issue:

your post appears to contain code that is not properly formatted as code

Please advise.

Why Python Descriptor Work for Class Level Attribute and not for an Instance Level Attribute.

class PropDescriptor:

    def __init__(self,*args):
        print("Init {} {}".format(type(self),args))
        self.value = 0

    def __get__ (self,instance,owner):
        print("get using descriptor")
        return instance.instance_att

    def __set__(self, instance, value):
        print("set using descriptor")
        instance.instance_att = value


class TestClass:
    class_att = PropDescriptor()

    def __init__(self):
        self.instance_att = PropDescriptor()



t = TestClass()
print("set instance level...")
t.instance_att = 3

print("\nget instance level...")
print(t.instance_att)

print("\nset class level...")
t.class_att = 4

print("\nget class level...")
print(t.class_att)

Output:

Init <class '__main__.PropDescriptor'> ()
Init <class '__main__.PropDescriptor'> ()
set instance level...

get instance level...
3

set class level...
set using descriptor

get class level...
get using descriptor
4

looks like the descriptor is not used for the instance_att.

I found this identical question, however I did not understand the answer.
Also read this, but I don't understand. It is also written here:

Instance Binding If binding to an object instance, a.x is transformed into the call: type(a).__dict__['x'].__get__(a, type(a)).

But: type(t).__dict__["instance_att"], raises an KeyError as type(t).__dict__

does not have such an attribute. It is an instance level att.

What am I missing?

5

1 Answer 1

2

After some attempts and try to reduce the post to an MCVE, I can confirm that if it's posted from a 1-rep account, this brings up the big red box says

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

:

[Stack Overflow][1]  
[Try it online][2]  
[Programming Puzzles & Code Golf][3]

[1]: https://stackoverflow.com
[2]: https://tio.run
[3]: https://codegolf.stackexchange.com

(which renders as

Stack Overflow
Try it online
Programming Puzzles & Code Golf

)

I guess Stack Overflow detects that by finding large block of text that contains lots of symbols (in this case, [ and ]). In this case it was the block of text

I found this [identical][1] question, however I did not understand the answer.  
Also read [this][2], but I don't understand.
It is also written [here][3]:

Temporary workaround: Use two newlines to split lines.

I found this [identical][1] question, however I did not understand the answer.

Also read [this][2], but I don't understand.
It is also written [here][3]:
1
  • But don't post it in the current form. (for the reason I said in the comment above)
    – user202729
    May 27, 2018 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .