简体   繁体   English

Python函数有很多参数

[英]Python function with a lot of arguments

I am new to Python and higher level languages in general, so I was wondering if it is looked down upon if I have a function that takes in a lot of arguments, and how to better architect my code to prevent this. 我一般都是Python和更高级语言的新手,所以我想知道如果我有一个接受大量参数的函数,以及如何更好地构建我的代码以防止这种情况。

For example what this function is essentially doing is printing each location of a string in a file. 例如,该函数基本上做的是在文件中打印字符串的每个位置。

def scan(fpin,base,string,pNo,print_to_file,dumpfile_path,quiet):

This function is being called from the main function, which is basically parsing the command line arguments and passing the data to the scan function. 从main函数调用此函数,该函数基本上解析命令行参数并将数据传递给扫描函数。 I have thought of creating a class containing all of these arguments and passing it to scan,but there will only be one instance of this data, so wouldn't that be pointless? 我曾想过创建一个包含所有这些参数并将其传递给扫描的类,但是这个数据只有一个实例,所以这不是没有意义的吗?

Named arguments are your friends. 命名参数是你的朋友。 For things that act like semi-optional configuration options with reasonable defaults, give the parameters the defaults, and only pass them (as named arguments) for non-default situations. 对于具有合理默认值的半可选配置选项,请为参数指定默认值,并仅为非默认情况传递它们(作为命名参数)。 If there are a lot of parameters without reasonable defaults, then you may want to name all of them when you call the function. 如果有许多参数没有合理的默认值,那么您可能希望在调用函数时为所有参数命名。

Consider the built-in function sorted . 考虑sorted的内置函数。 It takes up to four arguments. 它最多需要四个参数。 Is the reverse parameter before or after cmp ? cmp之前还是之后的reverse参数? What should I pass in as key if I want the default behavor? 如果我想要默认行为,我应该作为key传递什么? Answer: Hell if I can remember. 答:如果我记得的话,好吧。 I call sorted(A, reverse=True) and it does what I'd expect. 我调用sorted(A, reverse=True) ,它做了我所期望的。

Incidentally, if I had a ton of "config"-style arguments that I was passing into every call to scan, and only changing (say, fpin and string ) each time, I might be inclined to put all the other argumentsinto a dictionary, and then pass it to the function with **kwargs syntax. 顺便说一句,如果我有大量的“配置”式参数,我每次都要调用扫描,并且每次只更改(例如, fpinstring ),我可能倾向于将所有其他参数放入字典中,然后使用**kwargs语法将其传递给函数。 That's a little more advanced. 这有点先进。 See the manual for details. 有关详细信息,请参阅手册 (Note that this is NOT the same as declaring the function as taking **kwargs . The function definition is the same, the only difference is what calls to it look like.) (注意,这与将函数声明为使用**kwargs 。函数定义是相同的,唯一的区别是调用它看起来像。)

No, there's really nothing wrong with it. 不,这真的没什么不对。 If you have N different arguments (things that control the execution of your function), you have to pass them somehow - how you actually do that is just user preference if you ask me. 如果你有N个不同的参数(控制你的函数执行的东西),你必须以某种方式传递它们 - 如果你问我,你实际上如何做到这只是用户偏好。

However... if you find yourself doing something like this, though: 但是......如果你发现自己做了这样的事情,不过:

func('somestring', a=A, b=B, c=C)
func('something else', a=A, b=B)
func('something third', a=A, c=C, d=D)

etc. where A,B,C are really configurations for lots of different things, then you should start looking into a class. 其中A,B,C是很多不同事物的配置,那么你应该开始研究一个类。 A class does many things, but it does also create context. 一个类做了很多事情,但它也创建了上下文。 Instead, then you can do something like: 相反,那么你可以做类似的事情:

cf = myclass(a=A, b=B, c=C, d=D)
cf.func('somestring')
cf.func('something else')
cf.func('something third')

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM