问题标签 [setattr]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
python - % 属性前缀的 Python 用途
我见过具有类似属性的 Python 对象%values
。我无法访问这些属性,因为它们会引发SyntaxError
. 它们似乎也需要setattr
在没有以下内容的情况下创建SyntaxError
:
%
这里有什么目的或意义吗?为什么可以%values
设置 using setattr
,否则会引发 a SyntaxError
?
python - 如何在 Python 中正确使用 getattr 和 setattr?
我在 Python 中创建了一个名为 Student 的类。我需要创建该类的五个实例,然后能够从用户选择的实例中返回一个属性,并将一个属性设置为一个新值。这是我的代码:
我无法让我的 getattr 或 setattr 方法正常工作。我怎样才能让他们工作?
python - 父类在子类中使用的 settattr
我有一个图书馆,有一位家长和十几个孩子:
现在我想用一个简单的(但有点特别,用于另一种方法)方法来扩展该库。所以我想改变父类并使用它的孩子。
现在我可以像这样使用它:
或像这样:
所以,我的问题是:
- 这是好事吗?
- 这应该如何以更好的方式完成?
python - 嵌套子对象/链式属性上的 getattr 和 setattr?
我有一个对象 ( Person
),它有多个子对象 ( Pet, Residence
) 作为属性。我希望能够像这样动态设置这些子对象的属性:
目前我得到错误的输出:{'pet': <__main__.Pet object at 0x10c5ec050>, 'residence': <__main__.Residence object at 0x10c5ec0d0>, 'pet.name': 'Sparky', 'residence.type': 'Apartment'}
如您所见,不是在 的子对象上设置name
属性,而是在.Pet
Person
pet.name
Person
我无法指定
person.pet
,setattr()
因为不同的子对象将由相同的方法设置,如果/当找到相关键时,该方法会解析一些文本并填写对象属性。有没有一种简单/内置的方法来实现这一点?
或者我可能需要编写一个递归函数来解析字符串并
getattr()
多次调用,直到找到必要的子对象,然后调用setattr()
找到的子对象?
python - wtforms field created through setattr() loses properties
I am trying to create a form in python / Flask that will add some dynamic slider inputs to a set of standard fields. I am struggling to get it to work properly, though.
Most of the web forms in my app are static, created through wtforms as in:
When I am explicit like that, I can get the expected results by using the CritiqueForm()
in the view and passing the form object to render in the template.
However, I have a critique form that needs to dynamically include some sliders for rating criteria specific to a particular record. The number of sliders can vary form one record to the next, as will the text and IDs that come from the record's associated criteria.
When I looked for some ways to handle this, I found a possible solution from dezza (Dynamic forms from variable length elements: wtforms) by creating a class method in the form, which I could then call before instantiating the form I want to render. As in:
where 'append_slider' is always an IntegerField
with a label I provide. This works enough to allow me to populate the criteria sliders in the view, as in:
The ratings
list is built to give the template an easy way to reference the dynamic fields:
where render_slider_field
is a macro to turn the IntegerField into a slider.
With form.rating
—an integer field explicitly defined in CritiqueForm
—there is no problem and the slider is generated with a label, as expected. With the dynamic integer fields, however, I cannot reference the label
value in the integer field. The last part of the stack trace looks like:
Through some debugging, I have confirmed that none of the expected field properties (e.g., name, short_name, id ...) are showing up. When the dust settles, I just want this:
to be equivalent to this:
Is the setattr() technique inherently limiting in what information can be included in the form, or am I just initializing or referencing the field properties incorrectly?
EDIT: Two changes allowed my immediate blockers to be removed.
1) I was improperly referencing the form field in the template. The field parameters (e.g., label) appeared where expected with this change:
where I replace the string rating_field
with form[rating_field]
.
2) To address the problem of dynamically changing a base class from the view, a new form class ThisForm()
is created to extend my base CritiqueForm
, and then the dynamic appending is done there:
I don't know if this addresses the anticipated performance and data integrity problems noted in the comments, but it at least seems a step in the right direction.
python - 向 Python 类动态添加方法
我有一本类词典:
这里的 Class1、Class2 是我定义的 Python 类,每个类都有 foo() 和 bar() 方法。
假设我有另一个名为 OtherClass 的类。我想对上面的字典进行迭代,在OtherClass中动态添加一些方法如下:
这会按预期将方法 foo_cls1、bar_cls1、foo_cls2 和 bar_cls2 添加到 OtherClass。但看起来对 foo_cls1 和 bar_cls1 的调用分别被分派到 foo_cls2 和 bar_cls2 。也就是说,后来添加的方法定义“覆盖”了之前添加的方法定义(假设字典迭代顺序是 cls1 后跟 cls2)。为了进一步阐述问题,如果我在上述循环之后执行以下操作:
知道发生了什么,以及如何解决它?
python - python setattr 用于带有装饰器的动态方法创建器
我有一个定义了多个方法的类。
想象一下,我需要为此“Klass”填充 10 种方法。我想生成这些方法而不明确地全部写出来。所以我想做一个为每种方法做 setattr 的工厂。问题是我执行以下操作,最后一个方法具有最后一个值。每个都没有得到它的相关值,而是 value10。同样下面的解决方案没有实现装饰器,我不知道如何分配装饰器
所以当我跟随....
对于每种方法,这一切都会导致 value10。不明白为什么 ?另外,任何人都可以帮助如何实现具有一个属性的装饰器吗?PS:如果您有不使用“setattr”的建议,也将受到欢迎。
python - 如何在运行时将方法绑定到 python 中的对象?
因此,我想在运行时向对象添加方法。
但似乎 setattr 没有将方法绑定到对象。是否有可能做到这一点?
python - __getattr__ 在实现 __setattr__ 时抛出最大递归错误
我正在尝试使用魔术方法__getattr__
和创建一个圆类__setattr__
,我似乎有我的__getattr__
工作,但是当我实现时__setattr__
(如果值是一个int,它应该只允许设置x
和y
的值,并引发一个AttributeError
when用户尝试设置属性area
,circumference
和distance
to circle
), 我的__getattr__
会抛出最大递归错误。当我将其注释掉时,__getattr__
then 工作得很好。
和__setattr__
注释掉后,测试代码
打印出来: