参数 vs 参数 Python [英] Parameter vs Argument Python

查看:26
本文介绍了参数 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)

这里的参数是ab,传递的参数是54.

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),它将返回 False,因为 "hello" 是一个字符串.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆