如何将参数与函数对象关联起来?

发布于 2024-10-23 21:09:16 字数 297 浏览 0 评论 0原文

我是 python 新手,我发现“即使函数也是对象”这件事真的很酷,所以我只是在 PyShell 中使用函数。以下代码运行良好。

def add_num(a,b):
  c = a + b
  return c

x = add_num
x(5,2)

我想知道当我们分配 x = add_num 时是否可以存储参数。这样,每当我们调用 x() 时,它都会将 a 和 b(此处为 5 和 2)相加并返回结果。 x = add_num(5,2) 不起作用,因为 add_num(5,2) 实际上调用该函数并返回 7。

I am new to python and I find the "even functions are objects" thing really cool, so I was just playing with functions in the PyShell. The following code worked fine.

def add_num(a,b):
  c = a + b
  return c

x = add_num
x(5,2)

I was wondering whether we can store the parameters when we are assigning x = add_num. So that whenever we call x() it adds a and b (here 5 and 2) and returns the result. x = add_num(5,2) won't work since add_num(5,2) actually calls the function and returns 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝有风,海豚有海 2024-10-30 21:09:16

functools 模块中的 partial 函数 (文档)可以创建一个新的函数对象,其行为就像您想要的那样。

import functools

def add_num(a, b):
    c = a + b
    return c

x = functools.partial(add_num, 5, 2)
print(x()) # prints 7

您还可以仅指定一个参数,并在调用“partial”函数时提供另一个参数:

y = functools.partial(add_num, 5)
print(y(6)) # prints 11

它也适用于关键字参数:

z = functools.partial(add_num, b=5)
print(z(10)) # prints 15

正如注释中所建议的,另一种选择是使用 lambda 表达式 ( 文档),这只是创建新函数的简写。

f = lambda: add_num(25, 30)
print(f()) # prints 55

# the lambda definition is equivilent to
def f():
    return add_num(25, 30)

也可以像问题中所示那样修改原始函数,但在大多数情况下,这会被认为是不好的风格。如果您确实愿意,可以通过将参数添加为函数对象的 .func_defaults 属性(在旧版本的 Python 中称为 .__defaults__文档)。

add_num.func_defaults = (1, 2)
print(add_num()) # prints 3

The partial function in the functools module (documentation) can create a new function object behaving like you want.

import functools

def add_num(a, b):
    c = a + b
    return c

x = functools.partial(add_num, 5, 2)
print(x()) # prints 7

You can also specify only one argument, and provide the other one when calling the "partial" function:

y = functools.partial(add_num, 5)
print(y(6)) # prints 11

It works for keyword arguments too:

z = functools.partial(add_num, b=5)
print(z(10)) # prints 15

As suggested in the comments, another option is to use a lambda expression (documentation), which is just a shorthand for creating a new function.

f = lambda: add_num(25, 30)
print(f()) # prints 55

# the lambda definition is equivilent to
def f():
    return add_num(25, 30)

It is also possible to modify the original function like you show in the question, but in most cases this would be considered bad style. If you really want to, you can do so by adding the parameters as the .func_defaults attribute of the function object (called .__defaults__ in older versions of Python; documentation).

add_num.func_defaults = (1, 2)
print(add_num()) # prints 3
玩世 2024-10-30 21:09:16
def gen_add(x, y):
  def add():
    return x + y
  return add

fn =gen_add(2,4)
print fn
print fn()

fn1 = gen_add(5,4)
print fn1()

印刷:

<function add at 0x1004d4398>
6
9
def gen_add(x, y):
  def add():
    return x + y
  return add

fn =gen_add(2,4)
print fn
print fn()

fn1 = gen_add(5,4)
print fn1()

prints:

<function add at 0x1004d4398>
6
9
箹锭⒈辈孓 2024-10-30 21:09:16
import functools
x = functools.partial(add_num, 5, 2)
print x()
import functools
x = functools.partial(add_num, 5, 2)
print x()
那小子欠揍 2024-10-30 21:09:16
import functools
addnum = lambda x,y:x+y
print addnum(5,2) #print 7
z = functools.partial(addnum,5,2)
print z() #print 7
import functools
addnum = lambda x,y:x+y
print addnum(5,2) #print 7
z = functools.partial(addnum,5,2)
print z() #print 7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文