descriptor

How do I decorate an instance method with another class in Python 2.7?

…衆ロ難τιáo~ 提交于 2019-12-02 02:56:15
问题 In Python 2.7 I'd like to decorate an instance method test in class Foo with a decorator that is also a class called FooTestDecorator . From user Chirstop's question and the Python 2 docs' Descriptor HowTo guide I created this example. There seems to be an issue however, when I print my decorated method object, it's (inspected?) name is wrong because it is noted as a question mark like Foo.? . import types class FooTestDecorator(object): def __init__(self,func): self.func=func self.count=0 #

How do I decorate an instance method with another class in Python 2.7?

久未见 提交于 2019-12-02 02:02:25
In Python 2.7 I'd like to decorate an instance method test in class Foo with a decorator that is also a class called FooTestDecorator . From user Chirstop's question and the Python 2 docs' Descriptor HowTo guide I created this example. There seems to be an issue however, when I print my decorated method object, it's (inspected?) name is wrong because it is noted as a question mark like Foo.? . import types class FooTestDecorator(object): def __init__(self,func): self.func=func self.count=0 # tried self.func_name = func.func_name, but seemed to have no effect def __get__(self,obj,objtype=None):

Setting a descriptor in python3.5 asynchronously

社会主义新天地 提交于 2019-12-01 21:20:18
问题 I can write a descriptor returning a future which could be awaited on. class AsyncDescriptor: def __get__(self, obj, cls=None): # generate some async future here return future def __set__(self, obj, value): # generate some async future here return future class Device: attr=AsyncDescriptor() device=Device() Now I can get the value in a coroutine with value=await device.attr . How would I set this attribute? await device.attr=5 -> SyntaxError: can't assign to await expression await setattr

Setting a descriptor in python3.5 asynchronously

我的梦境 提交于 2019-12-01 19:45:26
I can write a descriptor returning a future which could be awaited on. class AsyncDescriptor: def __get__(self, obj, cls=None): # generate some async future here return future def __set__(self, obj, value): # generate some async future here return future class Device: attr=AsyncDescriptor() device=Device() Now I can get the value in a coroutine with value=await device.attr . How would I set this attribute? await device.attr=5 -> SyntaxError: can't assign to await expression await setattr(device, 'attr', 5) -> TypeError: object NoneType can't be used in 'await' expression device.attr=5 ->

Can a method be used as either a staticmethod or instance method?

柔情痞子 提交于 2019-12-01 18:03:40
I'd like to be able to do this: class A(object): @staticandinstancemethod def B(self=None, x, y): print self is None and "static" or "instance" A.B(1,2) A().B(1,2) This seems like a problem that should have a simple solution, but I can't think of or find one. It is possible, but please don't. I couldn't help but implement it though: class staticandinstancemethod(object): def __init__(self, f): self.f = f def __get__(self, obj, klass=None): def newfunc(*args, **kw): return self.f(obj, *args, **kw) return newfunc ...and its use: >>> class A(object): ... @staticandinstancemethod ... def B(self, x

Why do managed attributes just work for class attributes and not for instance attributes in python?

﹥>﹥吖頭↗ 提交于 2019-12-01 11:35:15
To illustrate the question check the following code: class MyDescriptor(object): def __get__(self, obj, type=None): print "get", self, obj, type return self._v def __set__(self, obj, value): self._v = value print "set", self, obj, value return None class SomeClass1(object): m = MyDescriptor() class SomeClass2(object): def __init__(self): self.m = MyDescriptor() x1 = SomeClass1() x2 = SomeClass2() x1.m = 1000 # -> set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> 10000 x2.m = 1000 # I guess that this overwrites the function. But why? # -> print x1.m # -

Meanings of dollar sign in Java method descriptor?

我的梦境 提交于 2019-11-30 09:35:15
For example, its part of the Jikes RVM stack. at [0x70cfba90, 0x708cfaa4] Lorg/apache/lucene/index/SegmentInfos; **access$000**(Ljava/lang/String;)V at [0x70cfbb04, 0x708b55c8] Lorg/apache/lucene/index/SegmentInfos$ FindSegmentsFile; run()Ljava/lang/Object; at line 554 at [0x70cfbb24, 0x708c4a8d] Lorg/apache/lucene/index/SegmentInfos; read(Lorg/apache/lucene/store/Directory;)V at line 272 'access' should be a method name. But I checked the class source code and its interfaces, there is no method there called 'access'. I couldn't find an answer on Google, since Google hates all kinds of

Eclipse 'loading descriptor' takes ages

偶尔善良 提交于 2019-11-30 05:58:12
We have a Java Spring MVC based project using Eclipse (Juno - the latest build), using the latest JVM 1.7 and Tomcat 7. Eclipse is pretty fast, and everything is set to default settings. Once it is all loaded up, it is lightning fast, which makes a pleasant change. However, the only gripe is that if I open a project, it begins 'Loading descriptor', which as far as I can tell is our 185-line web.xml file. Sometimes this might take 5 minutes to load, sometimes might just not load at all. This prevents any changes being made, as the system waits for the descriptor to load before anything else

Meanings of dollar sign in Java method descriptor?

有些话、适合烂在心里 提交于 2019-11-29 14:16:18
问题 For example, its part of the Jikes RVM stack. at [0x70cfba90, 0x708cfaa4] Lorg/apache/lucene/index/SegmentInfos; **access$000**(Ljava/lang/String;)V at [0x70cfbb04, 0x708b55c8] Lorg/apache/lucene/index/SegmentInfos$ FindSegmentsFile; run()Ljava/lang/Object; at line 554 at [0x70cfbb24, 0x708c4a8d] Lorg/apache/lucene/index/SegmentInfos; read(Lorg/apache/lucene/store/Directory;)V at line 272 'access' should be a method name. But I checked the class source code and its interfaces, there is no

How to implement __iadd__ for a Python property

霸气de小男生 提交于 2019-11-29 09:22:27
I'm trying to create a Python property where in-place adding is handled by a different method than retrieving the value, adding another value and reassigning. So, for a property x on an object o , o.x += 5 should work differently than o.x = o.x + 5 The value of o.x should be the same in the end, so as not to confuse people's expectations, but I want to make the in-place add more efficient. (In reality the operation takes a lot more time than simple addition.) My first idea was to define, in the class, x = property(etc. etc.) x.__iadd__ = my_iadd But this raises an AttributeError, presumably