Как различить свойство, устанавливаемое с помощью ___setattr__, внутри класса и вне класса? - PullRequest
1 голос
/ 11 июня 2019

Есть ли способ в __setattr__() различать набор атрибутов внутри класса или дочернего / наследующего класса и набор атрибутов вне текущего или дочернего класса?

Я хочу изменить способ установки атрибутов "извне", в моем случае создания модуля я хочу, чтобы при настройке атрибута у пользователя была другая логика, чем при установке из класса.

Например:
i.x = 5 должен присваивать 5 обычно при вызове из класса, и i является его экземпляром, но при вызове из другого класса он должен, скажем, вычесть 5 вместо 5.

Ответы [ 3 ]

4 голосов
/ 11 июня 2019

Немного низкоуровневый, но вы можете использовать inspect модуль:

import inspect

class A:

    def __init__(self):
        self.__x = 0

    @property
    def x(self):
        return self.__x

    @x.setter
    def x(self, value):
        f = inspect.currentframe()
        if 'self' in f.f_back.f_locals and issubclass(type(f.f_back.f_locals['self']), A):
            print('Called from class!')
            self.__x = -value
        else:
            print('Called from outside!')
            self.__x = value

    def fn(self):
        print('Calling A.x from inside:')
        self.x = 10

class B(A):
    def __init__(self):
        super().__init__()

    def fn2(self):
        print('Calling B.x from inside:')
        self.x = 15

a = A()
print("A.x after init:", a.x)
print('Calling A.x from outside')
a.x = 10
print("A.x called from the outside:", a.x)
a.fn()
print("A.x called from the inside:", a.x)

b = B()
print("B.x after init:", b.x)
print('Calling B.x from outside')
b.x = 20
print("B.x called from the outside:", b.x)
b.fn2()
print("B.x called from the inside:", b.x)

Отпечатки:

A.x after init: 0
Calling A.x from outside
Called from outside!
A.x called from the outside: 10
Calling A.x from inside:
Called from class!
A.x called from the inside: -10
B.x after init: 0
Calling B.x from outside
Called from outside!
B.x called from the outside: 20
Calling B.x from inside:
Called from class!
B.x called from the inside: -15
3 голосов
/ 11 июня 2019

Используйте свойство. Внутри класса вы можете назначить непосредственно базовый атрибут. Снаружи, присваивания x вместо этого уменьшают его.

class Foo:
    def __init__(self):
         self._x = 0

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x -= value
2 голосов
/ 11 июня 2019

Решение может состоять в том, чтобы всегда использовать self.__dict__ внутри класса без вызова метода __setattr__.

Пример:

class myClass:

    def __init__(self, value):
        self.__dict__['a'] = value

    def __setattr__(self, name, value):
        print("called from outside")
        if name == 'a':
            self.__dict__[name] = value - 5
        else:
            self.__dict__[name] = value


f = myClass(10)

print(f.a)
# 10

f.a = 20
print(f.a)
# called from outside
# 15
...