instance-method

How to understand the difference between class_eval() and instance_eval()?

六月ゝ 毕业季﹏ 提交于 2019-12-17 15:08:08
问题 Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘instance_bar’ for #<Foo:0x7dce8> Just based on the name of the methods, I would expect class_eval to allow you to add a class method to Foo and instance_eval to allow you to add an instance

Overcoming Python's limitations regarding instance methods

时光总嘲笑我的痴心妄想 提交于 2019-11-29 06:53:34
It seems that Python has some limitations regarding instance methods. Instance methods can't be copied. Instance methods can't be pickled. This is problematic for me, because I work on a very object-oriented project in which I reference instance methods, and there's use of both deepcopying and pickling. The pickling thing is done mostly by the multiprocessing mechanism. What would be a good way to solve this? I did some ugly workaround to the copying issue, but I'm looking for a nicer solution to both problems. Does anyone have any suggestions? Update: My use case: I have a tiny event system.

Adding attributes to instancemethods in Python

核能气质少年 提交于 2019-11-28 18:13:53
I bumped into this behaviour when trying to get class-decorators and method-decorators to play nicely together. Essentially, the method decorators would flag some of the methods as special with some dummy value, and the class decorator would come by after and fill in the value later. This is a simplified example >>> class cow: >>> def moo(self): >>> print 'mooo' >>> moo.thing = 10 >>> >>> cow.moo.thing 10 >>> cow().moo.thing 10 >>> cow.moo.thing = 5 AttributeError: 'instancemethod' object has no attribute 'thing' >>> cow().moo.thing = 5 AttributeError: 'instancemethod' object has no attribute

Overcoming Python's limitations regarding instance methods

柔情痞子 提交于 2019-11-28 00:13:40
问题 It seems that Python has some limitations regarding instance methods. Instance methods can't be copied. Instance methods can't be pickled. This is problematic for me, because I work on a very object-oriented project in which I reference instance methods, and there's use of both deepcopying and pickling. The pickling thing is done mostly by the multiprocessing mechanism. What would be a good way to solve this? I did some ugly workaround to the copying issue, but I'm looking for a nicer

How to understand the difference between class_eval() and instance_eval()?

懵懂的女人 提交于 2019-11-27 16:47:29
Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘instance_bar’ for #<Foo:0x7dce8> Just based on the name of the methods, I would expect class_eval to allow you to add a class method to Foo and instance_eval to allow you to add an instance method to Foo. But they seem to do the opposite . In the example above if you call class_bar on the Foo

Adding attributes to instancemethods in Python

放肆的年华 提交于 2019-11-27 10:48:31
问题 I bumped into this behaviour when trying to get class-decorators and method-decorators to play nicely together. Essentially, the method decorators would flag some of the methods as special with some dummy value, and the class decorator would come by after and fill in the value later. This is a simplified example >>> class cow: >>> def moo(self): >>> print 'mooo' >>> moo.thing = 10 >>> >>> cow.moo.thing 10 >>> cow().moo.thing 10 >>> cow.moo.thing = 5 AttributeError: 'instancemethod' object has

How can I use functools.singledispatch with instance methods?

非 Y 不嫁゛ 提交于 2019-11-26 21:56:34
Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation: from functools import singledispatch class TestClass(object): @singledispatch def test_method(arg, verbose=False): if verbose: print("Let me just say,", end=" ") print(arg) @test_method.register(int) def _(arg): print("Strength in numbers, eh?", end=" ") print(arg) @test_method.register(list) def _(arg): print("Enumerate this:") for i, elem in enumerate(arg): print(i, elem) if __name__ == '__main__': TestClass.test_method(55555) TestClass.test_method([33, 22,

How can I use functools.singledispatch with instance methods?

白昼怎懂夜的黑 提交于 2019-11-26 08:07:06
问题 Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation: from functools import singledispatch class TestClass(object): @singledispatch def test_method(arg, verbose=False): if verbose: print(\"Let me just say,\", end=\" \") print(arg) @test_method.register(int) def _(arg): print(\"Strength in numbers, eh?\", end=\" \") print(arg) @test_method.register(list) def _(arg): print(\"Enumerate this:\") for i, elem in