简体   繁体   English

在python中将多个参数传递给init类的最佳方法是什么?

[英]What is the best way to pass multiple parameters to class init in python?

I am creating a module in python that can take multiple arguments. 我在python中创建一个可以接受多个参数的模块。 What would be the best way to pass the arguments to the definition of a method? 将参数传递给方法定义的最佳方法是什么?

def abc(arg):
    ...

abc({"host" : "10.1.0.100", "protocol" : "http"})
def abc(host, protocol):
    ...

abc("10.1.0.100", "http")
def abc(**kwargs):
    ...

abc(host = "10.1.0.100", protocol = "http")

Or something else? 或者是其他东西?

Edit 编辑

I will actually have those arguments (username, password, protocol, host, password2) where none of them are required. 我实际上会有这些参数(用户名,密码,协议,主机,密码2),其中不需要它们。

def abc(host, protocol):
    ...

abc("10.1.0.100", "http")
abc(host="10.1.0.100", protocol="http")

If you call it using positional args or keyword args depends on the number of arguments and if you skip any. 如果你使用位置args或关键字args调用它取决于参数的数量,如果你跳过任何参数。 For your example I don't see much use in calling the function with keyword args. 对于您的示例,我没有看到使用关键字args调用函数有多少用处。


Now some reasons why the other solutions are bad: 现在其他解决方案不好的一些原因:

abc({"host" : "10.1.0.100", "protocol" : "http"})

This is a workaround for keyword arguments commonly used in languages which lack real keyword args (usually JavaScript). 这是一种常用于缺少真正的关键字args(通常是JavaScript)的语言中的关键字参数的解决方法。 In those languages it is nice, but in Python it is just wrong since you gain absolutely nothing from it. 在那些语言中它很不错,但在Python中它是错误的,因为你从中获得的一切都没有。 If you ever want to call the functions with args from a dict, you can always do abc(**{...}) anyway. 如果你想用dict调用带有args的函数,你总是可以做abc(**{...})

def abc(**kwargs):

People expect the function to accept lots of - maybe even arbitrary - arguments. 人们期望函数接受许多 - 甚至是任意的 - 参数。 Besides that, they don't see what arguments are possible/required and you'd have to write code to require certain arguments on your own. 除此之外,他们没有看到可能/需要的参数,你必须编写代码来自己要求某些参数。

If all the arguments are known ahead, use an explicit argument list, optionally with default values, like def abc(arg1="hello", arg2="world",...) . 如果提前知道所有参数,则使用显式参数列表,可选择使用默认值,例如def abc(arg1="hello", arg2="world",...) This will make the code most readable. 这将使代码最具可读性。

When you call the function, you can use either abd("hello", "world") or abc(arg1="hello", arg2="world") . 当你调用函数时,你可以使用abd("hello", "world")abc(arg1="hello", arg2="world") I use the longer form if there are more than 4 or 5 arguments, it's a matter of taste. 如果有超过4或5个参数,我会使用更长的形式,这是一个品味问题。

This really depends on the context, the design and the purpose of your method. 这实际上取决于方法的上下文,设计和目的。

If the parameters are defined and compulsory, then the best option is: 如果参数是定义和强制的,那么最好的选择是:

def abc(host, protocol):
    ...

abc('10.1.0.100', 'http')

in case the parameters are defined but optional, then: 如果参数已定义但是可选,则:

def abc(host=None, protocol=None): # I used None, but put a default value
    ...

you can call it thorugh positional arguments as abc('10.1.0.100', 'http') or by their name abc(protocol='http') 你可以把它称为abc('10.1.0.100', 'http')或他们的名字abc(protocol='http')

And if you don't know at first what are the arguments it will receive are (for example in string formatting) then the best option is using the **kwargs argument: 如果你一开始不知道它将接收的参数是什么(例如在字符串格式化中),那么最好的选择是使用**kwargs参数:

def abc(**kwargs):
    ...

And in this way you must call it using named arguments. 通过这种方式,您必须使用命名参数调用它。

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

相关问题 在python3中初始化类属性的最佳方法是什么? - What's the best way to init class attribute in python3? 将方法(带参数)传递给 python 中的另一个方法的最佳方法是什么 - What is the best way to pass a method (with parameters) to another method in python Python如何通过多重继承传递__init__参数 - How does Python pass __init__ parameters with multiple inheritance 在 Python 库上启动参数的最佳方法是什么? - What is the best way to initiate parameters on a Python library? 将多个参数传递给python中的函数的方法 - Way to pass multiple parameters to a function in python 在 OOP Python ZE5BA8B4C539C287BAAAEZ34 - What is the best way to use one function with same parameters in multiple classes in OOP Python tkinter 装饰Python类方法的最佳方法是什么? - What is the best way to decorate methods of a Python class? Python是处理多个线程的最佳方法是什么 - Python what is the best way to handle multiple threads 在 Python 中复制基类“__init__”签名的正确方法是什么? - What is the correct way of duplicating base class '__init__' signature in Python? 在类继承中使用默认值传递参数的最安全方法是什么? - What is the safest way to pass parameters with defaults in class inheritance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM