Python 入门(七)函数
什么是函数
我们知道圆的面积计算公式为:
S = πr²
当我们知道半径r的值时,就可以根据公式计算出面积。假设我们需要计算3个不同大小的圆的面积:
r1 = 12.34
r2 = 9.08
r3 = 73.1
s1 = 3.14 * r1 * r1
s2 = 3.14 * r2 * r2
s3 = 3.14 * r3 * r3
当代码出现有规律的重复的时候,你就需要当心了,每次写3.14 * x * x不仅很麻烦,而且,如果要把3.14改成3.14159265359的时候,得全部替换。
有了函数,我们就不再每次写s = 3.14 * x * x,而是写成更有意义的函数调用 s = area_of_circle(x)
,而函数 area_of_circle 本身只需要写一次,就可以多次调用。
抽象是数学中非常常见的概念。举个例子:
计算数列的和,比如:1 + 2 + 3 + ... + 100,写起来十分不方便,于是数学家发明了求和符号∑,可以把1 + 2 + 3 + ... + 100记作:
100
∑n
n=1
这种抽象记法非常强大,因为我们看到∑就可以理解成求和,而不是还原成低级的加法运算。
而且,这种抽象记法是可扩展的,比如:
100
∑(n²+1)
n=1
还原成加法运算就变成了:
(1 x 1 + 1) + (2 x 2 + 1) + (3 x 3 + 1) + ... + (100 x 100 + 1)
可见,借助抽象,我们才能不关心底层的具体计算过程,而直接在更高的层次上思考问题。
写计算机程序也是一样,函数就是最基本的一种代码抽象的方式。
Python不但能非常灵活地定义函数,而且本身内置了很多有用的函数,可以直接调用。
任务
此节没有任务,进入下一节继续学习。
调用函数
Python内置了很多有用的函数,我们可以直接调用。
要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数 abs,它接收一个参数。
可以直接从Python的官方网站查看文档:
http://docs.python.org/2/library/functions.html#abs
也可以在交互式命令行通过 help(abs) 查看abs函数的帮助信息。
调用 abs 函数:
>>> abs(100)
100
>>> abs(-20)
20
>>> abs(12.34)
12.34
调用函数的时候,如果传入的参数数量不对,会报TypeError的错误,并且Python会明确地告诉你:abs()有且仅有1个参数,但给出了两个:
>>> abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
如果传入的参数数量是对的,但参数类型不能被函数所接受,也会报TypeError的错误,并且给出错误信息:str是错误的参数类型:
>>> abs('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
而比较函数 cmp(x, y) 就需要两个参数,如果 x<y,返回 -1,如果 x==y,返回 0,如果 x>y,返回 1:
>>> cmp(1, 2)
-1
>>> cmp(2, 1)
1
>>> cmp(3, 3)
0
Python内置的常用函数还包括数据类型转换函数,比如 int()函数可以把其他数据类型转换为整数:
>>> int('123')
123
>>> int(12.34)
12
str()函数把其他类型转换成 str:
>>> str(123)
'123'
>>> str(1.23)
'1.23'
任务
sum()函数接受一个list作为参数,并返回list所有元素之和。请计算 1*1 + 2*2 + 3*3 + ... + 100*100。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlEAAAC1CAIAAADwVD00AAARyUlEQVR4nO2d3UtjZx6A80fszV6kkovCsNiFDghDbZUJFlEZKHTtbh03466zwtxIGHVqDUtWF4UOtQOawQ8sDCLdm7FBL+pFxNKLhPwV+QN6M+0MXmcvTpJzTnLefOg555fk9zw8DMnJSSZCkof3PV+RP/zxPURERA1GxN8BIiJiONI8RETUIs1DREQt0jxERNQizUNERC3SPERE1CLNQ0RELUbevHnz22+/Wf/W+P3339++ffvrr79GB2LolxEAAANlCAWaR/MAQB7pFmiB5tE8AJBHugVaoHk0DwDkkW6BFmgezQMAeaRboAWaR/MAQB7pFmgh8ubNm3fv3l1fX797947m0TwAEEG6BVqIvH379vr6+oMPPnBmj+bRPAAIE+kWaCFyfX393nvvDQ0NPXz4sJY9mkfzACBMpFughUgsFpuYmPj666+//fbbra0tK3s0j+YBQJhIt0ALka+++ur777//8ccfLy4ufv75559++un6+prm0TwACBPpFmghct0A4zyaBwAhI90CLXCsAs0DAHn8/Fk3vKwvy5u/c9//Ft+heTQPAOTx7Te9IUKBLu90HXFoHs0DAHl8+00Xap5p/W6D5tE8AJAnkN/3NvoUaaNVTdbv6HW6AZpH8wBAHp9/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" />
编写函数
在Python中,定义一个函数要使用 def 语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用 return 语句返回。
我们以自定义一个求绝对值的 my_abs 函数为例:
def my_abs(x):
if x >= 0:
return x
else:
return -x
请注意,函数体内部的语句在执行时,一旦执行到return时,函数就执行完毕,并将结果返回。因此,函数内部通过条件判断和循环可以实现非常复杂的逻辑。
如果没有return语句,函数执行完毕后也会返回结果,只是结果为 None。
return None可以简写为return。
任务
请定义一个 square_of_sum 函数,它接受一个list,返回list中每个元素平方的和。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjUAAADgCAIAAAB0GwB6AAAaDUlEQVR4nO2dz2sj6Z2H/UfsZQ9a40NgCE2gBwwNBhsLD45tBhqabojbU+7E3Q1zMabtboxF8NhgQ4b0GNzy2t34MBiThmBF2If0QcZJs5GiZTfJTCabZQMR2UuWzF42CX32HqpK9ZZUJamkqvp+bT0fnoNUfvWq1Fa/j98fVe/AP/zjPwEAAGhjQPwMAAAAmsFPAACgEfwEAAAawU8AAKAR/AQAABrBTwAAoBH8BAAAGsFPAACgEfwEAAAawU8AAKAR/AQAABrBTwAAoBH8BAAAGsFPAACgEfwEAAAaGfg/I3/961//9re/ff3115nBIYiLAUIICcklCQ9+wk+EELFIK0B18BN+IoSIRVoBqoOf8BMhRCzSClAd/ISfCCFikVaA6uAn/EQIEYu0AlRn4O9//zt+wk+EEJFIK0B1Bt69e1dXFH7CT4SQNCOtANUZuH//fl1R+Ak/EULSjLQCVGfghz/84fb2tq0o/ISfCCFpRloBqjPw5s2bn/3sZz/96U/fvXuHn/ATISTNSCtAdQbeuaH/hJ8IISlHWgGqw/py/EQIEUtsTXlIzcm9YwrBT/iJECKW2JrykKpifIv0g5/wEyFELLE15fgJ8BMhJMbE1pTjJ8BPhJAYE1tTPjBw6c42dXL8SgQ/4SdCiFhia8qN2sxqw45fiQy8MvLjH//417/+NX7CT4SQdJJUy34thvsGvv76f//yl6///Of/+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Ta4XzDV7DU978RP7E3Z4btdmf8LWdL174XD2rTmg18P8Uzzn02ENvdXffp9G9jmUQFoBqpPU+ghvzmlhv9zr+gjnKfsTdnxu12V/wlb0tE+g2Vdo2q9dbt/CVgbqrf5W+zSG1e/8opl/ShRpBahOgvsTxnH9E/sThrz1dd+fMIQY9wk0qgpqlFPetzBkP8a46g/bp5F9DuWRVoDqKL9/BPtrXCuSvphG2z6BMe43KFJ/huufkkdaAaqDnxIAP4XS8/6EYWjbJzDu/QbTrt+F+0ckjbQCVAc/JQD7E7aA++9dDbj/XkpIK0B1lPsJAOA6I60A1cFPAABiSCtAdfATAIAY0gpQHfwEACCGtAJUBz8BAIghrQDVwU8AAGJIK0B18BMAgBjSClAd/AQAIIa0AlQHPwEAiCGtANXBTwAAYkgrQHXwEwCAGNIKUB38BAAghrQCVAc/AQCIIa0A1cFPAABiSCtAdfATAIAY0gpQHfwEACCGtAJUBz8BAIghrQDVwU8AAGJIK0B18BMAgBjSClAd/AQAIIa0AlQHPwEAiCGtANXBTwAAYkgrQHXwEwCAGNIKUJ3/B0ZM5bgo6dIBAAAAAElFTkSuQmCC" alt="" />
返回多值
函数可以返回多个值吗?答案是肯定的。
比如在游戏中经常需要从一个点移动到另一个点,给出坐标、位移和角度,就可以计算出新的坐标:
# math包提供了sin()和 cos()函数,我们先用import引用它:
import math
def move(x, y, step, angle):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
这样我们就可以同时获得返回值:
>>> x, y = move(100, 100, 60, math.pi / 6)
>>> print x, y
151.961524227 70.0
但其实这只是一种假象,Python函数返回的仍然是单一值:
>>> r = move(100, 100, 60, math.pi / 6)
>>> print r
(151.96152422706632, 70.0)
用print打印返回结果,原来返回值是一个tuple!
但是,在语法上,返回一个tuple可以省略括号,而多个变量可以同时接收一个tuple,按位置赋给对应的值,所以,Python的函数返回多值其实就是返回一个tuple,但写起来更方便。
任务
一元二次方程的定义是:ax² + bx + c = 0
请编写一个函数,返回一元二次方程的两个解。
注意:Python的math包提供了sqrt()函数用于计算平方根。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAloAAAD+CAIAAABtKzxsAAAf1UlEQVR4nO2dT2sbWbqH8yG8uQuN0KKhuYS7aDCE8YyMjRuPHXrpMGMcxfcmGHoTzNgBY3HxxOBAN6QXThk7wYtemFlZCHsxvZDpIQsJfQp/gFndmSFr30Wp/rylOqU6UqlelfQ8PItYLh2Vy+7316fqVL2P5v7jN4iIiDPuI/U9QEREVJc4REREJA4RERGJQ0RExDniEBERcY44REREnCMOERER54hDRETEOeIQERFxjjhEREScIw4RERHniENERMQ54hAREXGOOERERJwjDhEREeeIQ0RExDniEBERcY44REREnCMOERER54hDRETEuf/4zaP/8/jnP//5r3/96x//+EepXMGsfAQAYOABJgnikDgEAB206z8IiEPiEAB00K7/ICAOiUMA0EG7/oOAOCQOAUAH7foPAuKQOAQAHbTrPwiIQ+IQAHTQrv8gePTvf/+bOCQOASB/tOs/CB59+fLFTUTikDgEgDzRrv8gePTu3Ts3EYlD4hAA8kS7/oPg0d///ve//e1vX758IQ6JQwDIk3GV9WkP2jH9gI++fPnC7JA4BID8GUtNn/YsdBnHj8nKUuIQAHTIvqDHjWn7WWPat/D4VntiOmKZ7yRxSBwCgA7ZVvOHuITwX0n5cbbbW2H7gydvmfkeEofEIQDokHE1N2ehaYMRtx+O9MPa7vCIEIfEIQDokGEpfyAOR4Y4JA4BQIcMS/nD9MZhwrHKdg+JQ+IQAHTIsJQ/TFIcJv+w6YcNvzH2XdkeQ+KQOAQAHTIs5Q+TFIdWnzLKG7PdQ+KQOAQAHTIs5Q/E4cjkHYd7dw8Pd2/Sbvnw8HD/aV070ohDABgHGZbyXkFPzLOBn2i7/XCkH3bEH8eWyY3DUrlS2rsjDgFgWsm2mj+kvu8w4dNtt89qJ2PHj+zDwHFG3bGJPllKHALA9JJtNe/V9BRhk/zptttb7Vvsj58wvulYjePo5R2H6x/ve7ND92To3ZveSdGH+4un3mZPP937O+j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OAOERE1FG7/oOAOERE1FG7/oOAOERE1FG7/oOAOERE1FG7/oOAOERE1FG7/oOAOERE1FG7/oOAOERE1FG7/oPg/wFvTVX/RmROzwAAAABJRU5ErkJggg==" alt="" />
递归函数
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可以看出:
fact(n) = n! = 1 * 2 * 3 * ... * (n-1) * n = (n-1)! * n = fact(n-1) * n
所以,fact(n)可以表示为 n * fact(n-1),只有n=1时需要特殊处理。
于是,fact(n)用递归的方式写出来就是:
def fact(n):
if n==1:
return 1
return n * fact(n - 1)
上面就是一个递归函数。可以试试:
>>> fact(1)
1
>>> fact(5)
120
>>> fact(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
如果我们计算fact(5),可以根据函数定义看到计算过程如下:
===> fact(5)
===> 5 * fact(4)
===> 5 * (4 * fact(3))
===> 5 * (4 * (3 * fact(2)))
===> 5 * (4 * (3 * (2 * fact(1))))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120
递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。
使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出。可以试试计算 fact(10000)。
任务
汉诺塔 (http://baike.baidu.com/view/191666.htm) 的移动也可以看做是递归函数。
我们对柱子编号为a, b, c,将所有圆盘从a移到c可以描述为:
如果a只有一个圆盘,可以直接移动到c;
如果a有N个圆盘,可以看成a有1个圆盘(底盘) + (N-1)个圆盘,首先需要把 (N-1) 个圆盘移动到 b,然后,将 a的最后一个圆盘移动到c,再将b的(N-1)个圆盘移动到c。
请编写一个函数,给定输入 n, a, b, c,打印出移动的步骤:
move(n, a, b, c)
例如,输入 move(2, 'A', 'B', 'C'),打印出:
A --> B
A --> C
B --> C
定义默认参数
定义函数的时候,还可以有默认参数。
例如Python自带的 int() 函数,其实就有两个参数,我们既可以传一个参数,又可以传两个参数:
>>> int('123')
123
>>> int('123', 8)
83
int()函数的第二个参数是转换进制,如果不传,默认是十进制 (base=10),如果传了,就用传入的参数。
可见,函数的默认参数的作用是简化调用,你只需要把必须的参数传进去。但是在需要的时候,又可以传入额外的参数来覆盖默认参数值。
我们来定义一个计算 x 的N次方的函数:
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
假设计算平方的次数最多,我们就可以把 n 的默认值设定为 2:
def power(x, n=2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
这样一来,计算平方就不需要传入两个参数了:
>>> power(5)
25
由于函数的参数按从左到右的顺序匹配,所以默认参数只能定义在必需参数的后面:
# OK:
def fn1(a, b=1, c=2):
pass
# Error:
def fn2(a=1, b):
pass
任务
请定义一个 greet() 函数,它包含一个默认参数,如果没有传入,打印 'Hello, world.',如果传入,打印 'Hello, xxx.'
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmAAAACiCAIAAABptuYRAAATB0lEQVR4nO2dX0sk6b2A/RDn5lz0Nn0RGELWiwNzk44OyoaODnMjWeG42nh2hmAuMi5HJ+vaBMPArCRsn4C0+IddITB4pSsOzA5EzV4pfgo/wN7M7gxeey6q6//vra7qrqpfVe/z8LB0V3dXlcapJ29VddXIf/znB4iIiBhwRH0NEBERCyiBREREFCSQiIiIggQSERFRkEAiIiIKEkhERETBkbdv3/7444/Wfx1++umnd+/e/fDDD5VqDdNyBADAwB0UDwJJIAFAH+0WgACBJJAAoI92C0CAQBJIANBHuwUgQCAJJADoo90CECCQBBIA9NFuAQiMvH379v3797e3t+/fvyeQBBIAVNBuAQiMvHv37vb29sMPP/Q2kkASSADIE+0WgMDI7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MjSG8sncezbXfva7eF1tOxxsHFwvKY+zbTCDJCLp6OWEa1WwACOR6DHNIbJvv1H4M0DfvmZnyBdA9V1jcHDiQ3cEYso9otAIH8zmJd3E3WyMBfT3FvmOwztIvVid9Y4+BCOH8nsBvWa3+B5AbOiGVUuwUgkOPXPB7vXF7vLA4SyELeMNmvL5CVscZB93Dj6PKhp3aBEWSqgeQ8HcQyqt0CEMgtkFOt06vL3Sf97mIt7g2T/ZpGkPXNC2d6fTPeCNK/9zWuHINELKPaLQCBTAM51Trt80uQ4RFkUW+YHND8Pci5GXv6wvJc4yBGIH1z4yxWxKFWuwUgUJIr6RT1hslFk/2riCVVuwUgUJJAFvOGyUWTK+kgllbtFoBASQJZxBsmF02uxYpYYrVbAAKlCSQi4hCr3QIQIJCIiPpqtwAECCQior7aLQABAomIqK92C0CAQCIi6qvdAhAgkIiI+mq3AAQIJCKivtotAAECiYior3YLQIBAIiLqq90CECCQiIj6arcABAgkIqK+2i0AAQKJiKivdgtAgEAiIuqr3QIQIJCIiPpqtwAECCQior7aLQABAomIqK92C0CAQCIi6qvdAhAgkIiI+mq3AAQIJCKivtotAAECiYior3YLQIBAIiLqq90CEPh/gAiLOduLwrEAAAAASUVORK5CYII=" alt="" />
定义可变参数
如果想让一个函数能接受任意个参数,我们就可以定义一个可变参数:
def fn(*args):
print args
可变参数的名字前面有个 * 号,我们可以传入0个、1个或多个参数给可变参数:
>>> fn()
()
>>> fn('a')
('a',)
>>> fn('a', 'b')
('a', 'b')
>>> fn('a', 'b', 'c')
('a', 'b', 'c')
可变参数也不是很神秘,Python解释器会把传入的一组参数组装成一个tuple传递给可变参数,因此,在函数内部,直接把变量 args 看成一个 tuple 就好了。
定义可变参数的目的也是为了简化调用。假设我们要计算任意个数的平均值,就可以定义一个可变参数:
def average(*args):
...
这样,在调用的时候,可以这样写:
>>> average()
0
>>> average(1, 2)
1.5
>>> average(1, 2, 2, 3, 4)
2.4
任务
请编写接受可变参数的 average() 函数。
aaarticlea/png;base64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pheGQNoLu2OKzWyv7jdON6VR8xnqeetbzzL6dwF83VKnXs5jOUrunC5QhITAc0kbQHUt8Nls5Ta0+Yz3P3nasXM9Toh2fsa3NUH02VDvcP4NUkTaC7giu5+k+kZba/TPW88xpW88z83bcJ+pi758N3I4B4xshVaSNoDujMT8I68UUcnmd63mOajt0NsJQSBtBd/DZCKFoPc9RbSfP/CBwJaSNoDv4bKTQs57nqML8jXAlpI2gO6PhMwCAUUDaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLqDzwAAbEHaCLrz/wFLJhifEO0JcQAAAABJRU5ErkJggg==" alt="" />
Python 入门(七)函数的更多相关文章
- Python入门篇-函数、参数及参数解构
Python入门篇-函数.参数及参数解构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.函数概述 1>.函数的作用即分类 函数 数学定义:y=f(x) ,y是x的函数,x ...
- Python入门之 函数
Python入门之 函数 1.初识函数 1.1 什么是函数? <1> 将某个功能封装到一个空间中就是一个函数 <2> 减少重复代码 1.2 定义函数 def -- python ...
- Python入门-初始函数
今天让我们来初步认识一个在python中非常重要的组成部分:函数 首先,让我们来幻想这样一个场景: 比如说我们现在想要通过社交软件约一个妹子,步骤都有什么? print('打开手机 ') print( ...
- python入门15 函数
函数的主要作用是实现代码复用 1 python内置函数 2 匿名函数lambda 3 自定义函数 def functionname(arg):... #coding:utf-8 #/usr/bin/p ...
- Python入门之函数的装饰器
本章目录: 装饰器: 一.为什么要用装饰器 二.什么是装饰器 三.无参装饰器 四.装饰器语法糖 五.认证装饰器实现 六.叠加多个装饰器 七.带参装饰器 ======================== ...
- python基础(七)函数
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 函数最重要的目的是方便我们重复使用相同的一段程序. 将一些操作隶属于一个函数,以后 ...
- python入门总结-函数
函数形式: def functionname(paramlist): function body 局部变量不改变实参的值,如果需要改变,声明global.比如,global x 可以给函数默认值,注意 ...
- python入门之函数
为什么要用函数 python的函数是由一个新的语句编写,即def ,def是可执行的语句--函数并不存在,知道python运行了def后才存在. 函数是通过赋值函数传递的,参数通过赋值传递给函数. d ...
- Python入门之函数的嵌套/名称空间/作用域/函数对象/闭包函数
本篇目录: 一.函数嵌套 二.函数名称空间与作用域 三.函数对象 四.闭包函数 ============================================================ ...
- Python入门day04_函数与装饰器
一.函数入门 什么是函数: # 函数:函数是一系列代码的集,用来完成特定功能的代码块,类似于工具,可以重复不但的去使用 为什么要有函数: # 优点:# 1. 避免代码的冗余 # 2. 让程序代码结构更 ...
随机推荐
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- Java调用存储过程小结
学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下.(关于调用sqlserver的存储过程将在下次进行小结请关注) 一:无返回值的存储过程 存储过程 ...
- 无法识别的属性 configProtectionProvider的解决方案
用RsaProtectedConfigurationProvider加密数据库连接字符串时,只要App.config有任何改动,都会提示无法识别的属性 configProtectionProvider ...
- Ubuntu 下查看中文man手册方法
转载自:http://blog.chinaunix.net/uid-24830506-id-3266493.html Ubuntu 中文man手册安装方法 分类: LINUX Ubuntu 下查看中文 ...
- 一站式学习Wireshark(九):应用Wireshark显示过滤器分析特定数据流(上)
介绍 掌握显示过滤器对于网络分析者来说是一项必备的技能.这是一项大海捞针的技巧.学会构建,编辑,保存关键的显示过滤器能够节省数小时的时间. 与捕捉过滤器使用的BPF语法不同,显示过滤器使用的是Wire ...
- 集群同步hive的脚本
程序员就是把一切手工做的事情变成让计算机来做,从而可以让自己偷偷懒. 以下就是个非常low的hive文件夹同步程序,至于节点超过100个或者1000个的,可以加个循环了. #!/bin/sh #=== ...
- skynet1.0阅读笔记2_skynet的消息投递skynet.call
为了了解 skynet.call 的调用过程,需要先看看 skynet的队列是如何把包分到不同工作线程的.看下图 查看 global_queue 的skynet_globalmq_push和skyne ...
- KBEngine 服务器端-loginapp-协议构建、解析执行详细介绍
宏宏宏 由于 C++ 是静态语言,不能像 js 一样通过函数名字符串来直接执行函数,所以将 messageId 映射到可执行函数的复杂性大大提升:KBEngine 使用了一系列精巧的「宏」来解决这个问 ...
- kbengine + cocos2d-js-demo理解
KBEngine 是国内开源的游戏服务器引擎,据说参考了 Bigworld 的架构:网上能找到的开源游戏服务器引擎很少,网易的 Pomelo 是用 Node.js 来实现的,现在还是觉得 C/C++ ...
- navicat导入csv
1.navicat for mysql 导入csv时出错,主要是由于csv中包含汉字所致: 2.解决办法为再host新连接时,选择高级,然后在编码菜单里选择20936 (Simplified Chin ...