C
C
cherish52021-12-19 03:06:09
Python
cherish5, 2021-12-19 03:06:09

Zero out the values ​​of all initialized descriptors?

Good afternoon. You need to create two objects and store the values. The Price class contains a null class method that assigns 0 to all descriptor fields of the owner objects. And when a value is removed from the owner object (using the del operator), the reference from the Price class to this object is also removed. There was a difficulty with how to reset. How to do it?

class Price(object):

    def __init__(self):
        self.instances = []
        self.val = None

    def __get__(self, obj, obj_type):
        return self.val

    def __set__(self, obj, val):
        self.val = val
        self.instances.append(obj)   # можно ли так хранить ссылки на все объекты-владельцы?   и как к ним обратиться из null()

    def null(self):
        pass

    def __delete__(self, instance):
        self.value = 'erased'

class Customer:
    score = Price()


t1 = Customer()
t2 = Customer()
t1.score = 5          #5
t2.score = 10        #10
Price.null()
print(t1.score, t2.score)    #   0  0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-12-19
@Vindicar

Well, in general - why are you doing this? But if you really want to...
Do you want to reset only fields of the Price type? Then you do not need to store references to the owners, it is enough to store references to all instances of Price and assign them value = None or whatever you use there as a label for the remote value.
But frankly, this is a strange decision.

C
cherish5, 2021-12-19
@cherish5

Vindicar It's a simple task - turn numbers inside different objects to 0 with one command. Understand descriptors, decide, pass.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question