問題タブ [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.

0 投票する
2 に答える
216 参照

python - % 属性プレフィックスの Python の目的

のような属性を持つ Python オブジェクトを見てきました%values。.を発生させるため、そのような属性にはアクセスできませんSyntaxError。またsetattr、なしで作成する必要があるようですSyntaxError:

%ここに目的や意味はありますか?%valuesを使用して設定できるのにsetattr、そうでない場合は が発生するのはなぜSyntaxErrorですか?

0 投票する
2 に答える
330 参照

python - 子で使用する親クラスの settattr

私は1人の親と1ダースの子供を持つライブラリを持っています:

ここで、そのライブラリを単純な (ただし、別のアプローチで使用するために少し特殊な) メソッドで拡張したいと考えています。そこで、親クラスを変更してその子を使用したいと思います。

そして今、私はそれを次のように使うことができます:

またはこのように:

だから、私の質問は次のとおりです。

  1. これは良いことですか?
  2. これをより良い方法で行うにはどうすればよいですか?
0 投票する
11 に答える
31756 参照

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アトリビュートを設定する代わりに、 に新しいアトリビュートが作成されます。PetPersonpet.namePerson

  • 関連するキーが見つかった場合にテキストを解析し、オブジェクト属性を埋める同じメソッドによって異なるサブオブジェクトが設定されるため、person.petto を指定することはできません。setattr()

  • これを達成するための簡単な/組み込みの方法はありますか?

  • または、文字列を解析しgetattr()、必要なサブオブジェクトが見つかるまで複数回呼び出しsetattr()てから、見つかったサブオブジェクトを呼び出す再帰関数を作成する必要があるのでしょうか?

0 投票する
1 に答える
1964 参照

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.

0 投票する
0 に答える
325 参照

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 であると仮定します)。問題をさらに詳しく説明するには、上記のループの後に次のようにします。

何が起こっているのか、それを修正する方法はありますか?

0 投票する
2 に答える
6270 参照

python - メソッドを使用した setattr および getattr

いくつかのアクションを参照クラスに委譲するボイラー プレート クラスがあります。次のようになります。

これはrefClassです:

Python Metaprogramming を使用して、これをよりエレガントで読みやすくしたいのですが、方法がわかりません。

setattrgetattrについて聞いたことがありますが、次のようなことができると思います

そして、私はどこかからこれを行う必要があることを知っています、私は推測します:

私はこの概念を完全に理解することはできません。コードを繰り返さないこと、および関数型プログラミングで for ループを使用してメソッドを生成することについての基本は理解していますが、このメソッドを他の場所から呼び出す方法がわかりません。ひーえーぷ!

0 投票する
3 に答える
1458 参照

python - 実行時にPythonでメソッドをオブジェクトにバインドするにはどうすればよいですか?

このように、実行時にオブジェクトにメソッドを追加したいと考えています。

しかし、setattr はメソッドをオブジェクトにバインドしていないようです。これを行うことは可能ですか?

0 投票する
2 に答える
2349 参照

python - __setattr__ の実装時に __getattr__ が最大の再帰エラーをスローする

__getattr__魔法のメソッドとを使用してサークル クラスを作成しようとしていますが、機能__setattr__しているように見えますが、__getattr__実装すると(値が int の場合にと__setattr__の値のみを設定し、 whenを発生させる必要があります)。ユーザーが属性、、およびをに設定しようとすると、最大の再帰エラーがスローされます。コメントアウトすると、then は正常に機能します。xyAttributeErrorareacircumferencedistancecircle__getattr____getattr__

__setattr__コメントアウトすると、テスト コードは次のようになります。

プリントアウト: