2007/1/21

今朝、私は一つ本を読みました。 あの人の個性を相談の本わちょと興味かある。僕の個性は多分狂放型や耿直型や勇敢型などかある。実は、僕は僕について未だ全部分かるけど。ようやく僕はよく頑張っだった。

以前留下來的...

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;