5

Say I define this descriptor:

class MyDescriptor(object):

   def __get__(self, instance, owner):
        return self._value

    def __set__(self, instance, value):
        self._value = value

    def __delete__(self, instance):
        del(self._value)

And I use it in this:

class MyClass1(object):
    value = MyDescriptor()


>>> m1 = MyClass1()
>>> m1.value = 1
>>> m2 = MyClass1()
>>> m2.value = 2
>>> m1.value
2

So value is a class attribute and is shared by all instances.

Now if I define this:

class MyClass2(object)
    value = 1

>>> y1 = MyClass2()
>>> y1.value=1
>>> y2 = MyClass2()
>>> y2.value=2
>>> y1.value
1

In this case value is an instance attribute and is not shared by the instances.

Why is it that when value is a descriptor it can only be a class attribute, but when value is a simple integer it becomes an instance attribute?

2 Answers 2

11

You're ignoring the instance parameter in your implementation of MyDescriptor. That is why it appears to be a class attribute. Perhaps you want something like this:

class MyDescriptor(object):

    def __get__(self, instance, owner):
        return instance._value

    def __set__(self, instance, value):
        instance._value = value

    def __delete__(self, instance):
        del(instance._value)
5
  • How would you implement MyDescriptor() to make it behave like an instance variable? May 10, 2011 at 3:26
  • 1
    Did you try what I suggested? May 10, 2011 at 3:28
  • 1
    It worked. Thanks! My previous comment was made before I saw the updates you made to your answer. May 10, 2011 at 3:32
  • Great! I usually try to get the answer up before going off to test sample code. :) May 10, 2011 at 3:34
  • Continuation: Simple, just operate on the instance :)
    – richo
    May 10, 2011 at 3:35
0

Will not work if you try the code below:

class MyClass1(object):
    value = MyDescriptor()
    value2 = MyDescriptor()

c = MyClass1()
c.value = 'hello'
c.value2 = 'world'

# where c.value also equals to "world"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.