2011年11月9日 星期三

[Python] property decorator

正常用法是http://docs.python.org/library/functions.html#property

以下是鬼打牆用法...

應該把Dgram裡面的started改名字才對~

似乎在舊得python可以work


class Base(object):
a = 1
def __init__(self, listener, handle=None, backlog=None, spawn='default'):
self._xxx = 'xxx'
# the faulure is because of started name is used by derived object Dram
self.started = None

class Dgram(Base):
b = 2
def __init__(self, listener, handle=None, backlog=None, spawn='default', **args):
Base.__init__(self, listener, handle=handle, backlog=backlog, spawn=spawn)
self.delay = 123

@property
def started(self):
return self._recv_event is not None or self._start_receving_timer is not None

def echo(msg, addr):
print 'echo ' + msg

if __name__ == '__main__':
s = Dgram('123', echo)

ytshen@ytshen-ThinkCentre-A58:~/temp$ python test_obj.py
Traceback (most recent call last):
File "test_obj.py", line 23, in
s = Dgram('123', echo)
File "test_obj.py", line 12, in __init__
Base.__init__(self, listener, handle=handle, backlog=backlog, spawn=spawn)
File "test_obj.py", line 6, in __init__
self.started = None
AttributeError: can't set attribute

沒有留言:

張貼留言