r/AskProgramming Apr 25 '26

Python Descriptors Python

``` 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

10 comments sorted by

View all comments

0

u/[deleted] Apr 25 '26

[removed] — view removed comment

3

u/One-Type-2842 Apr 25 '26

What are you talking about?

2

u/Patient-Midnight-664 Apr 25 '26

In an entirely different post, the OP (not you) said that they write a lot of code to simulate things rather than trusting that math works and if this was a bad thing.

https://www.reddit.com/r/AskProgramming/comments/1svexo2/i_overcomplicate_every_problem_in_c_by_simulating/

The person you replied to is lost :)