本文介绍了参数vs参数Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对Python还是很陌生,对于使用参数还是参数我仍然感到困惑.例如,如何编写一个接受字符串作为参数的函数?

So I'm still pretty new to Python and I am still confused about using a parameter vs an argument. For example, how would I write a function that accepts a string as an argument?

推荐答案

通常,当人们说参数/参数时,它们是同一意思,但是它们之间的主要区别在于参数是函数中声明的参数,而参数是调用函数时传递的内容.

Generally when people say parameter/argument they mean the same thing, but the main difference between them is that the parameter is what is declared in the function, while an argument is what is passed through when calling the function.

def add(a, b):
    return a+b

add(5, 4)

这里,参数是 a b ,传递的参数是 5 4

Here, the parameters are a and b, and the arguments being passed through are 5 and 4.

由于Python是一种动态类型化的语言,因此在声明函数时我们不需要声明参数的类型(与其他语言(如C)不同).因此,我们无法控制将确切的类型作为参数传递给函数.例如,在上面的函数中,我们可以执行 add("hello","hi").

Since Python is a dynamically typed language, we do not need to declare the types of the parameters when declaring a function (unlike in other languages such as C). Thus, we can not control what exact type is passed through as an argument to the function. For example, in the above function, we could do add("hello", "hi").

这是诸如 isinstance()之类的函数的所在很有帮助,因为它们可以确定对象的类型.例如,如果您执行 isinstance("hello",int),则由于"hello" 是字符串,它将返回False.

This is where functions such as isinstance() are helpful because they can determine the type of an object. For example, if you do isinstance("hello", int), it will return False since "hello" is a string.

这篇关于参数vs参数Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:35