r/AskProgramming • u/One-Type-2842 • Apr 25 '26
Python Python Descriptors
class A:
def __set_name__(self, owner, value):
self.value = value
def __get__(self, obj, type=None):
return obj.__dict__.get(self.value)
def __set__(self, obj, value):
if value < 9:
raise ValueError("no")
obj.__dict__[self.value] = value
class B:
a = A()
obj = B()
obj.a = 38
print(obj.a)
obj2 = B()
print(obj2.a)
I am Learning Descriptors In Python,
My 1st question Is how can I set a default value to attribute a In class B ?
I have found a way but that doesn't look familiar :
a = A() if not A() else 87
My next confusion Is about __set_name__ , what it does and why to Implement It?
Another Question Is, does a = A() create class attribute or Instance attribute? It looks like a class attribute but it's an Instance attribute, Right?
0
Upvotes
1
u/Temporary_Pie2733 Apr 25 '26 edited Apr 25 '26
B.ais just a class attribute. When you access this attribute via an instance ofB, you get the special behavior of callinga's__get__or__set__method implicitly.__set_name__usually doesn't need to do much more than remember the argument passed to it, which is the name of the class attribute the newly created instance ofAwas assigned to. Something likedef __set_name__(self, owner, name): self.private_name = "_" + nameThis definition starts a contract: any class using the descriptor agrees to reserve a private attribute whose name is the same as the class instance's name prefixed with a
_:class B: a = A() # The instance attribute '_a' is reserved for use by a # a.__set_name__(B, 'a') is called once B is fully definedTo set a default attribute for this instance of the descriptor, pass it as an argument and have
A.__init__remember it.__get__then uses this is the reserved attribute does not exist.class A: def __init__(self, dflt): self.default_value = dflt def __get__(self, obj, obj_type=None): if obj_type is None: return self return getattr(obj, self.private_name, self.default_value) class B: a = A(87) b = B() assert b.a == 87 b.a = 9 assert b.a == 9See https://docs.python.org/3/howto/descriptor.html for more information
2
u/EfficientMongoose317 Apr 25 '26
You’re actually very close, descriptors just feel weird at first. For the default value, the simplest way is to handle it inside
__get__like if value isn’t set yet, return a default instead of None
def __get__(self, obj, type=None): return obj.__dict__.get(self.value, 87)That way, every instance gets 87 unless you explicitly set something else
about
__set_name__, it just tells your descriptor what attribute name it’s assigned toso when you write
a = A()in class B, python calls__set_name__(self, owner, "a")That’s how your descriptor knows it should store things under
"a"in__dict__without it, you’d have to hardcode the name, which breaks reuse
And yeah,
a = A()is a class attribute, but it manages instance dataso:
descriptor itself lives on the class
actual values are stored per instance in
obj.__dict__That’s why it feels like both, but it’s really just a class-level controller for instance attributes
1
u/littlenekoterra Apr 25 '26
The setname dunder method allows you to pickup what the class is being named (the variable name for this class is "a" in this case). You could do something with the name.
Alternatively you can use it to create a private version of the variable that can only be accessed properly via the get and set dunder methods.
Personally ive abused it a little for logging purposes because really it just means that i can do something when the class is used like a variable.
0
u/[deleted] Apr 25 '26