全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
1349 2
2015-05-05
A function is a device to group a set of statements so they can be run more than once in a program.

def is executable code: your function does not exist until Python reaches and runs the def. in typical operation, def statements are
coded in module files and are naturally run to generate fucntions when the module files they reside in is first imported.

def creates an object and assigns it to a name.

lambda creates an object but returns it as a result

return sends a result object back to the caller

yield sends a result object back to the caller, but remembers where it left off.

global declares module-level variables that are to be assigned

nonlocal declares enclosing function variables that are to be assigned.

arguments are passed by assignment (object reference):

arguments are passed by postion, unless you say otherwise: values you pass in a function call match names in a function
defintion from left to right by default. function calls can also pass arguments by name with name=value keyword syntax,
and unpack arbitrarily many arguments to send with *pargs and **kargs starred-argument notation.

arguments, return values, and variables are not declared





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-5-5 04:04:23
arguments are passed by automatically assigning objects to local variable names:  function arguments are just
python assignment at work. because references are implemented as pointers, all arguments are in effect , passed
by pointers. objects passed as arguments are never automatically copied

assigning to argument names inside a function does not affect the caller.

changing a mutable object argument in a function may impact the caller.

Immutable arguments are effectively passed "by value" and "mutable arguments" are passed "by pointer".
to mimick passing by value for mutable arguments, we can do
def changer(a,b):
   b=b[:]  # copy list, so we do not impact calller

or changer(a, tuple(L)) # pass a tuple, os changes are errors

tools are match arguments:
1. Positionals: matched from left to right
2. Keywords: matched by argument name:
3. Defaults: specify values for optional arguments that aren't passed
4. varargs collecting: collecting arbitrarily many positional or keyword arguments: functions canuse special arguments preceded with one or two * characters to collect an arbitrary number of possibly extra arguments.
5. varargs unpacking: pass arbitrarily many positional or keyword arguments

examples:
func(value)
func(name=value)
func(*iterable)
func(**dict) # pass key/value pair
def func(name)
def func(name=value)
def func(*name)
def fund(**name)
def func(*other, name)
def func(*, name=value)






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-5-5 04:15:04
unpacking arguments:

def func(a,b,c,d): print(a,b,c,d)

args=(1,2)
args +=(3,4)
func(*args)

or

args ={'a':1, 'b':2, 'c':3}
args['d']=4
func(**args)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群