python - does pep 232 also works for instance methods? -


I was curious whether (properties of the function) also works for class methods. In the end, I think this is not doing anything wrong or not?

  Python 2.7.6 (Default, February 26, 2014, 12:07:17) [GCC 4.8.2 20140206 (Prerelease)] "Help" for more information on Linux2, "Copyright "," Credit "or" License "type & Gt; & Gt; & Gt; Square Fu (object): ... DIF A (self): ... print (auto.bar) ... & gt; & Gt; & Gt; F = Foo ()> gt; & Gt; & Gt; F.a.bar = "bar" traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; AttributeError: There is no attribute 'bar' in the 'instancemethod' object  

Example methods just Wrappers are used to work, in fact, only the work becomes due to methods.

You can always use the underlying task:

  fa__func __. Bar = 'bar'  

instancemethod .__func___ attribute is the underlying function object.

Once set, method cover proxy objects, you can not set them directly on the cover:

  & gt; & Gt; & Gt; F.a.__func__.bar = 'bar' & gt; & Gt; & Gt; F.a.bar 'bar' & gt; & Gt; & Gt; In Python 2, you can also use  examplemethod.im_func , but for further compatibility with Python 3 it is recommended that you enter  __func __ . . 

This is clearly recorded in:

The methods also support arbitrary function properties (but not set) on the built-in function object.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -