2006/12/27

decorator

http://www.donews.net/limodou/archive/2004/12/19/207521.aspx

說明很容易懂,最後也舉了一些使用的想法,這一篇的上面有實踐的例子,蠻好的

2006/12/21

property

property packages up three functions that get, set, or delete an attribute, and a docstring.
property(fget=None, fset=None, fdel=None, doc=None)

property() is a succinct way of building a data descriptor that triggers function calls upon access to an attribute

The property() builtin helps whenever a user interface has granted attribute access and then subsequent changes require the intervention of a method.

For example, if you want to define a size attribute that's computed, but also settable, you could write:

class C(object):
def get_size (self):
result = ... computation ...
return result
def set_size (self, size):
... compute something based on the size
and set internal state appropriately ...

# Define a property. The 'delete this attribute'
# method is defined as None, so the attribute
# can't be deleted.
size = property(get_size, set_size,
None,
"Storage size of this instance")

how property() is implemented in terms of the descriptor protocol, here is a pure Python equivalent:
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc

def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
return self.fget(obj)

def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
self.fset(obj, value)

def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
self.fdel(obj)



2006/12/20

Tuples are not read-only lists

Tuples usually indicate a heterogenous collection, for example (first_name, last_name) or (ip_address, port). Note that the type may be same, maynot be same. Sometimes think of a tuple as a row in a relational database

heterogenous
adj
consisting of elements that are not of the same kind or nature;

lambda forms & locally-defined function

Functions are already first class objects in Python, and can be declared in a local scope. Therefore the only advantage of using a lambda form instead of a locally-defined function is that you don't need to invent a name for the function -- but that's just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!


In computer science, a programming language is said to support first-class functions if it treats functions as first-class objects. Specifically, this means that functions can be created during the execution of a program, stored in data structures, passed as arguments to other functions, and returned as the values of other functions.

Generator expressions

A generator expression is a compact generator notation in parentheses:

generator_expression ::= "(" test genexpr_for ")"
genexpr_for ::= "for" expression_list "in" test [genexpr_iter]
genexpr_iter ::= genexpr_for | genexpr_if
genexpr_if ::= "if" test [genexpr_iter]

A generator expression yields a new generator object. The iterating values would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to yield a value that is reached the innermost block for each iteration.
innermost
adj
1: being deepest within the self;
2: situated or occurring farthest within;


List Comprehensions

Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses.
The result will be a list resulting from evaluating the expression in the context of the for and if clauses which follow it. If the expression would evaluate to a tuple, it must be parenthesized.

[...for...]
>>> [str(round(355/113.0, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']

2006/12/18

raw string

Raw string literals are parsed in exactly the same way as ordinary string literals; it's just the conversion from string literal to string object that's different. This means that all string literals must end with an even number of backslashes; otherwise, the unpaired backslash at the end escapes the closing quote character, leaving an unterminated string.

processors consider an unmatched trailing backslash to be an error anyway, so raw strings allow you to pass on the string quote character by escaping it with a backslash.

to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.

The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with "r". So r"\n" is a two-character string containing "\" and "n", while "\n" is a one-character string containing a newline.

>>> r'ss\n'
'ss\\n'
>>> 'ss\n'
'ss\n'
>>> r'ss\n'
'ss\\n'
>>> 'ss\n'
'ss\n'
>>> 'sss\'
File "", line 1
'sss\'
^
SyntaxError: EOL while scanning single-quoted string

>>> r'sss\'
File "", line 1
r'sss\'
^
SyntaxError: EOL while scanning single-quoted string

2006/12/16

《管子、牧民》中有“礼、义、廉、耻,国之四维;四维不张,国乃灭亡。”

礼是什么?是国家的制度规范。制度规范就是要给人们树立一个什么可干、什么不可干的标准界限。有了礼,人们就知道行为的节限。但是,在社会中,总有一部分 人想方设法地超越和破坏国家的制度规范,以谋取自己的私利。当这样的人总是得不到应有的惩罚的时候,超越和破坏国家制度规范的人就会越来越多;当这种人多 到一定程度的时候,国家的制度规范也就名存实亡了。管子所说的国家倾斜不正,就是指的这种情况。
   义是什么?是一种“应当”的标准,在管子这里主要是指政治伦理标准。国家任用官员,都有一定的机制和办法,既有推荐,也有提拔,而不是自己去跑官要 官,或者用不正当的手段把别人踩下去,自己挤上来。在行政工作中,应当是各守其位,各负其责,而不应超越职权。这些都是政治中的“应当”。在现实中,总是 会有一部分人想方设法地用一切不正当的手段升官。跑关系,行贿赂,造谣中伤、打击诬陷比自己强的人,是这些人惯用的伎俩。如果这样的人屡屡得手,他们就不 会专心政务,就会更加起劲地跑关系,行贿赂,造谣中伤、打击诬陷比自己强的人。与之相反,有政绩的人就必然不能得到提拔,必然就会心灰意冷,也同样不会专 心于政务工作。国家的制度规范名存实亡,政治伦理标准不能得到贯彻,国家就必然会处于一个十分危险、随时可能颠覆的境地。
   廉是什么?是官员的政治品质。现在,我们所说的廉主要是指廉洁,即不行贿受贿,不贪赃枉法,不图谋私利,不以权换钱,不依仗权利挥霍享乐。而管子所 说的廉,除了自己要具备这方面的政治道德品质之外,还包括不容忍别人去行贿受贿、贪赃枉法、图谋私利。国家的制度没有了,政治伦理标准没有了,官员没有起 码的廉德,国家也就不成其为国家了。
   耻是什么?是官员和国民的道德感。耻,也就是一种羞耻之心,即知好知坏、知善知恶、知是知非的道德良心,即不肯跟着别人做恶事的那种内心信念和最后 的心理防线。在我们的社会中,有些人没有起码的道德感和羞耻之心,做了坏事,得了便宜,便沾沾自喜;见别人也做了坏事,占的便宜比自己大,便心生羡慕,暗 自鼓励自己以后胆子要更大一些。当别人做坏事受到了惩罚,就暗自庆幸自己幸运;当自己受到了惩罚,就感叹自己运气不好,倒霉的事偏偏找上了自己。这种人不 把干坏事看成是一件羞耻的事,反而看成是一种“本事”。如果这种心理像瘟疫一样地传染开来,并成为一种普遍的社会心理的话,这个国家就完了,这个民族也就 完了。