楼主: Enthuse
1387 2

Python: functions [推广有奖]

  • 4关注
  • 39粉丝

已卖:995份资源

大师

8%

还不是VIP/贵宾

-

威望
0
论坛币
75391 个
通用积分
825.9999
学术水平
103 点
热心指数
114 点
信用等级
86 点
经验
299244 点
帖子
12952
精华
0
在线时间
5848 小时
注册时间
2007-4-7
最后登录
2024-1-22

楼主
Enthuse 发表于 2015-5-5 03:43:30 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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





二维码

扫码加我 拉你入群

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

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

关键词:Functions function python tions CTI statements naturally operation function generate

沙发
Enthuse 发表于 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)






藤椅
Enthuse 发表于 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)

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-25 19:11