問題タブ [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
を使用して設定できるのにsetattr
、そうでない場合は が発生するのはなぜSyntaxError
ですか?
python - 子で使用する親クラスの settattr
私は1人の親と1ダースの子供を持つライブラリを持っています:
ここで、そのライブラリを単純な (ただし、別のアプローチで使用するために少し特殊な) メソッドで拡張したいと考えています。そこで、親クラスを変更してその子を使用したいと思います。
そして今、私はそれを次のように使うことができます:
またはこのように:
だから、私の質問は次のとおりです。
- これは良いことですか?
- これをより良い方法で行うにはどうすればよいですか?
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
to を指定することはできません。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 - メソッドを使用した setattr および getattr
いくつかのアクションを参照クラスに委譲するボイラー プレート クラスがあります。次のようになります。
これはrefClassです:
Python Metaprogramming を使用して、これをよりエレガントで読みやすくしたいのですが、方法がわかりません。
setattrとgetattrについて聞いたことがありますが、次のようなことができると思います
そして、私はどこかからこれを行う必要があることを知っています、私は推測します:
私はこの概念を完全に理解することはできません。コードを繰り返さないこと、および関数型プログラミングで for ループを使用してメソッドを生成することについての基本は理解していますが、このメソッドを他の場所から呼び出す方法がわかりません。ひーえーぷ!
python - 実行時にPythonでメソッドをオブジェクトにバインドするにはどうすればよいですか?
このように、実行時にオブジェクトにメソッドを追加したいと考えています。
しかし、setattr はメソッドをオブジェクトにバインドしていないようです。これを行うことは可能ですか?
python - __setattr__ の実装時に __getattr__ が最大の再帰エラーをスローする
__getattr__
魔法のメソッドとを使用してサークル クラスを作成しようとしていますが、機能__setattr__
しているように見えますが、__getattr__
実装すると(値が int の場合にと__setattr__
の値のみを設定し、 whenを発生させる必要があります)。ユーザーが属性、、およびをに設定しようとすると、最大の再帰エラーがスローされます。コメントアウトすると、then は正常に機能します。x
y
AttributeError
area
circumference
distance
circle
__getattr__
__getattr__
__setattr__
コメントアウトすると、テスト コードは次のようになります。
プリントアウト: