python语法重点参考

一个学习各种脚本语言的网站(IT加油站),方便reference
http://www.fzs8.net/python/


Python 在函数中接收元组和列表

当要使函数接收元组或字典形式的参数的时候,有一种特殊的方法,它分别使用和**前缀。这种方法在函数需要获取可变数量的参数的时候特别有用。

>>> def powersum(power,
args):
…     ‘’’Return the sum of each argument raised to specified power.’’’
…     total = 0
…     for i in args:
…          total += pow(i, power)
…     return total

>>> powersum(2, 3, 4)
25

>>> powersum(2, 10)
100

由于在args变量前有前缀,所有多余的函数参数都会作为一个元组存储在args中。如果使用的是前缀,多余的参数则会被认为是一个字典的键/值对。


Python 继承

Python不会自动调用基本类的constructor,你得亲自专门调用它。

class SchoolMember:
     ‘’’Represents any school member.’’’
     def init(self, name, age):
self.name = name
self.age = age

         print ‘(Initialized SchoolMember: %s)’ % self.name

     def tell(self):
         ‘’’Tell my details.’’’
         print ‘Name:"%s" Age:"%s"’ % (self.name, self.age),

class Teacher(SchoolMember):
     ‘’’Represents a teacher.’’’
     def init(self, name, age, salary):
SchoolMember.init(self, name, age)
self.salary = salary

         print ‘(Initialized Teacher: %s)’ % self.name

     def tell(self):
SchoolMember.tell(self)

         print ‘Salary: "%d"’ % self.salary



python中的@:函数修饰符&静态成员函数

在 Python2.4之后,系统中加入了函数修饰符这个东西,使用@符号作为指示器,用来修饰函数。一个修饰符就是一个函数,它以被修饰的函数作为参数,返回修饰后的同名函数或者其它可调用的对象。
修饰符必须出现在函数定义前一行,而不允许和函数定义在同一行。下面的两条语句中,第二条是非法的:

@A
def f():

#error:
@A def f():

修饰器可以自定义,只需要符合上面所说的规定,传入一个函数为参数,返回修饰后的同名函数或者其它可调用的对象。下面就是这样的一个例子。在命令行中运行就可以看到,函数 f 有一个属性是attr,值是 decorated。

def deco(func):
   func.attr = ‘decorated‘
return func

@deco
def f(): pass

>>> f
<function f at 0x402ef0d4>
>>> f.attr
‘decorated‘


使用 @staticmethod 修饰符来修饰函数即可
class jfo:
def init(self):
print "init ok"

def func(self):
print "member"

@staticmethod
def func2():
print "static"



修饰符@举例

http://www.python.org/dev/peps/pep-0318/

The current syntax for function decorators as implemented in Python 2.4a2 is:

@dec2
@dec1
def func(arg1, arg2, …):
pass

This is equivalent to:

def func(arg1, arg2, …):
pass
func = dec2(dec1(func))

@decomaker(argA, argB, …)
def func(arg1, arg2, …):
pass

This is equivalent to:

func = decomaker(argA, argB, …)(func)



Define a class with a singleton instance:

def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance

@singleton
class MyClass:
…Add attributes to a function:

def attrs(
kwds):
def decorate(f):
for k in kwds:
setattr(f, k, kwds[k])
return f
return decorate

@attrs(versionadded="2.2",
author="Guido van Rossum")
def mymethod(f):


Enforce function argument and return types:
def accepts(
types):
def check_accepts(f):
assert len(types) == f.func_code.co_argcount
def new_f(args, **kwds):
for (a, t) in zip(args, types):
assert isinstance(a, t),
"arg %r does not match %s" % (a,t)
return f(
args, kwds)
new_f.func_name = f.func_name
return new_f
return check_accepts

def returns(rtype):
def check_returns(f):
def new_f(*args,
kwds):
result = f(args, **kwds)
assert isinstance(result, rtype),
"return value %r does not match %s" % (result,rtype)
return result
new_f.func_name = f.func_name
return new_f
return check_returns

@accepts(int, (int,float))
@returns((int,float))
def func(arg1, arg2):
return arg1
arg2