- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 __setattr__()
中有什么方法可以区分来自类或子/继承类内部的属性集,以及来自当前类或子类外部的属性集?
我想从“外部”更改设置属性的工作方式,在我制作模块的情况下,我希望用户在设置属性时与在类内部设置属性时具有不同的逻辑。
例如:i.x = 5
当从类中调用时通常应该分配 5 并且 i
是它的一个实例,但是当从另一个类中调用时它应该减去 5 而不是设置为 5。
最佳答案
有点低级,但你可以使用 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
关于python - 如何区分类内和类外使用 __setattr__ 设置的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56550263/
type.__setattr__ 用于类,基本上是元类的实例。另一方面,object.__setattr__ 用于类的实例。这是完全明白的。 我看不出这两种方法有什么显着差异,至少在 Python 级
我将命名元组用于不可变数据结构,直到遇到数据类,我在我的用例中更喜欢它(与问题无关)。 现在我了解到它们不是一成不变的!至少严格来说不是。setattr(frozen_dc_obj, "prop",
我试图了解 __setattr__ 如何与类属性一起工作。当我试图覆盖 __setattr__ 以防止在简单类中写入属性时,这个问题就出现了。 我的第一次尝试使用了实例级属性,如下所示: class
我定义了以下类(其中很大一部分被缩短),但我的主要问题是,一旦用其初始化变量定义了此类的对象,我想确保这些变量在类外部是不可更改的...如果进行这样的尝试,我将需要引发 AssertionError
只有当在对象的属性中找不到该属性时,我才想使用 __setattr__,例如 __getattr__。 我真的必须使用try- except吗? def __setattr__(self, name,
如果在类中重写魔术方法 __getattr__,则只有在找不到属性时才调用被重写的方法,否则会出现默认行为。 但是__setattr__好像不是这样,它影响所有的属性。 我正在寻找的是当属性存在时表现
如何在旧式类上执行 __setattr__ 的等效操作? 最佳答案 如果你想做的是为旧样式类的实例设置属性,你可以使用 setattr内置函数,它也应该适用于旧式类。示例 - >>> class Fo
如下面的代码所示,为什么我不能使用 __setattr__ 在作为重载方法的类的一部分的字典上设置值?我预计 b.hello 不会存在。 class MyClass(): datastore
我研究 Python 已经有一段时间了,我开始明白正确覆盖 __setattr__ 可能很麻烦(to say the least !)。 有哪些有效的方法可以确保/向我自己证明覆盖已正确完成?我特别关
在 Python 中,我知道可以这样说 >>> class C: ... def __setattr__(self, name, value): ... print("Hey, you c
我知道在 Python 中可以在运行时向类添加方法: class Test: def __init__(self): self.a=5 test=Test() import t
在 Python 类中,当我使用 __setattr__ 时,它优先于此类(或任何基类)中定义的属性。考虑以下代码: class Test(object): def get_x(self):
我正在尝试覆盖 Python 类的 __setattr__ 方法,因为我想在每次实例属性更改其值时调用另一个函数。但是,我不希望在 __init__ 方法中出现这种行为,因为在此初始化期间,我设置了一
我有以下代码: #-*-coding:utf-8-*- class A(object): def __init__(self, x): self.x = x def _
在 werkzeug 中,我们看到: class Local(object): __slots__ = ('__storage__', '__ident_func__') def __
我想定义一个返回 __setattr__ 的通用实现的函数,该函数使属性不可设置,除非它们是属性(我应该一般测试数据描述符,但那是稍后的事情) )。该函数如下所示: def make_setattr(
我的应用程序在 Windows 上运行,我正在尝试针对 Linux 对其进行配置。 我得到以下 AttributeError: Preferences instances has no attribu
我正在实现一个类的所有预定义函数,当我实现我的__setattr__() 来更新列表时我卡住了 class testop: def __init__(self): self.l
我有一个类的属性,我可以在检查其他属性后更改这些属性,所以我编写了这个类: class MyClass(): def __init__(self, x): self.x = x
在 __setattr__() 中有什么方法可以区分来自类或子/继承类内部的属性集,以及来自当前类或子类外部的属性集? 我想从“外部”更改设置属性的工作方式,在我制作模块的情况下,我希望用户在设置属性
我是一名优秀的程序员,十分优秀!