跟老齐学Python之总结参数的传递
就前面所讲,函数的基本内容已经完毕。但是,函数还有很多值得不断玩味的细节。这里进行阐述。
参数的传递
python中函数的参数通过赋值的方式来传递引用对象。下面总结通过总结常见的函数参数定义方式,来理解参数传递的流程。
def foo(p1,p2,p3,...)
这种方式最常见了,列出有限个数的参数,并且彼此之间用逗号隔开。在调用函数的时候,按照顺序以此对参数进行赋值,特备注意的是,参数的名字不重要,重要的是位置。而且,必须数量一致,一一对应。第一个对象(可能是数值、字符串等等)对应第一个参数,第二个对应第二个参数,如此对应,不得偏左也不得偏右。
>>> def foo(p1,p2,p3):
... print "p1==>",p1
... print "p2==>",p2
... print "p3==>",p3
...
>>> foo("python",1,["qiwsir","github","io"]) #一一对应地赋值
p1==> python
p2==> 1
p3==> ['qiwsir', 'github', 'io']
>>> foo("python")
Traceback (most recent call last):
File "
TypeError: foo() takes exactly 3 arguments (1 given) #注意看报错信息
>>> foo("python",1,2,3)
Traceback (most recent call last):
File "
TypeError: foo() takes exactly 3 arguments (4 given) #要求3个参数,实际上放置了4个,报错
def foo(p1=value1,p2=value2,...)
这种方式比前面一种更明确某个参数的赋值,貌似这样就不乱子了,很明确呀。颇有一个萝卜对着一个坑的意味。
还是上面那个函数,用下面的方式赋值,就不用担心顺序问题了。
>>> foo(p3=3,p1=10,p2=222)
p1==> 10
p2==> 222
p3==> 3
也可以采用下面的方式定义参数,给某些参数有默认的值
>>> def foo(p1,p2=22,p3=33): #设置了两个参数p2,p3的默认值
... print "p1==>",p1
... print "p2==>",p2
... print "p3==>",p3
...
>>> foo(11) #p1=11,其它的参数为默认赋值
p1==> 11
p2==> 22
p3==> 33
>>> foo(11,222) #按照顺序,p2=222,p3依旧维持原默认值
p1==> 11
p2==> 222
p3==> 33
>>> foo(11,222,333) #按顺序赋值
p1==> 11
p2==> 222
p3==> 333
>>> foo(11,p2=122)
p1==> 11
p2==> 122
p3==> 33
>>> foo(p2=122) #p1没有默认值,必须要赋值的,否则报错
Traceback (most recent call last):
File "
TypeError: foo() takes at least 1 argument (1 given)
def foo(*args)
这种方式适合于不确定参数个数的时候,在参数args前面加一个*,注意,仅一个哟。
>>> def foo(*args): #接收不确定个数的数据对象
... print args
...
>>> foo("qiwsir.github.io") #以tuple形式接收到,哪怕是一个
('qiwsir.github.io',)
>>> foo("qiwsir.github.io","python")
('qiwsir.github.io', 'python')
上一讲中已经有例子说明,可以和前面的混合使用。此处不赘述。
def foo(**args)
这种方式跟上面的区别在于,必须接收类似arg=val形式的。
>>> def foo(**args): #这种方式接收,以dictionary的形式接收数据对象
... print args
...
>>> foo(1,2,3) #这样就报错了
Traceback (most recent call last):
File "
TypeError: foo() takes exactly 0 arguments (3 given)
>>> foo(a=1,b=2,c=3) #这样就可以了,因为有了键值对
{'a': 1, 'c': 3, 'b': 2}
下面来一个综合的,看看以上四种参数传递方法的执行顺序
>>> def foo(x,y=2,*targs,**dargs):
... print "x==>",x
... print "y==>",y
... print "targs_tuple==>",targs
... print "dargs_dict==>",dargs
...
>>> foo("1x")
x==> 1x
y==> 2
targs_tuple==> ()
dargs_dict==> {}
>>> foo("1x","2y")
x==> 1x
y==> 2y
targs_tuple==> ()
dargs_dict==> {}
>>> foo("1x","2y","3t1","3t2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {}
>>> foo("1x","2y","3t1","3t2",d1="4d1",d2="4d2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {'d2': '4d2', 'd1': '4d1'}
通过上面的例子,看官是否看出什么名堂了呢?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Delivery Optimization is a feature that helps Windows Update and Windows Store run and deliver updates faster. Cache files in Delivery Optimization are supposed to be deleted after a while, but for some of our readers they keep piling up and taking up unnecessary space. Is it safe to delete delivery optimization files? Yes, it is safe to delete delivery optimization files, and in this article, you will find out how easy it is to do so in Windows 11. Although it is not recommended to manually delete delivery optimization files, it is possible to do so automatically. How to delete delivery optimization files on Windows 11? Click the search bar, type Disk Cleanup, and open the tool from the results. If you have multiple drives, select the drive with your system (usually C:

New feature of PHP5.4 version: How to use callable type hint parameters to accept callable functions or methods Introduction: PHP5.4 version introduces a very convenient new feature - you can use callable type hint parameters to accept callable functions or methods . This new feature allows functions and methods to directly specify the corresponding callable parameters without additional checks and conversions. In this article, we will introduce the use of callable type hints and provide some code examples,

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

During the development process, we may encounter such an error message: PHPWarning: in_array()expectsparameter. This error message will appear when using the in_array() function. It may be caused by incorrect parameter passing of the function. Let’s take a look at the solution to this error message. First, you need to clarify the role of the in_array() function: check whether a value exists in the array. The prototype of this function is: in_a

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores

C++ parameter type safety checking ensures that functions only accept values of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

Although large-scale language models (LLM) have strong performance, the number of parameters can easily reach hundreds of billions, and the demand for computing equipment and memory is so large that ordinary companies cannot afford it. Quantization is a common compression operation that sacrifices some model performance in exchange for faster inference speed and less memory requirements by reducing the accuracy of model weights (such as 32 bit to 8 bit). But for LLMs with more than 100 billion parameters, existing compression methods cannot maintain the accuracy of the model, nor can they run efficiently on hardware. Recently, researchers from MIT and NVIDIA jointly proposed a general-purpose post-training quantization (GPQ).
