有更好的方法吗?就像我传递func
中将在inside_func
函数中使用的参数一样?
def inside_func(arg1,arg2):
print arg1, arg2
return
def func(arg1, arg2):
inside_func(arg1,arg2)
return
答案 0 :(得分:6)
当然是。
您的外部功能提供服务,并且为了完成其工作,可能需要输入才能使用。 如何使用这些输入取决于该功能。如果它需要另一个函数来完成它们的工作并且它们逐字传递参数,那么就是一个实现细节。
您在这里只做标准封装和模块化。这将是任何语言的正确编程实践,而不仅仅是Python。
Python标准库充满了例子;它通常用于为快速用例提供更简单的界面。例如textwrap.wrap()
function:
def wrap(text, width=70, **kwargs):
"""Wrap a single paragraph of text, returning a list of wrapped lines.
Reformat the single paragraph in 'text' so it fits in lines of no
more than 'width' columns, and return a list of wrapped lines. By
default, tabs in 'text' are expanded with string.expandtabs(), and
all other whitespace characters (including newline) are converted to
space. See TextWrapper class for available keyword args to customize
wrapping behaviour.
"""
w = TextWrapper(width=width, **kwargs)
return w.wrap(text)
除了将参数传递给其他callables之外别无其他,只是因此您的代码不必记住如何使用TextWrapper()
类进行快速的一次性文本换行工作。