简体   繁体   English

Python中的可选参数

[英]Optional Arguments in Python

What are the advantages of having Optional args in Python. 在Python中拥有Optional args有什么优势。 Instead of overloading one function (or method) with args + optional args, wouldn't Polymorphism with Inheritance suffice? 而不是用args +可选args重载一个函数(或方法),具有继承的多态性就足够了吗?

I am just trying to understand the burning reason to have this feature. 我只是想了解具有此功能的迫切原因。 or is it the case of being able to do one thing many ways? 还是能够以多种方式做一件事情?

PS: I can see that it makes sense to have it in functional programming, to avoid having to define many functions practically doing almost the same thing, but are there any other... PS:我看到在函数式编程中使用它是有道理的,以避免必须定义实际上执行几乎相同操作的许多函数,但是还有其他任何...

Optional args have little to do with polymorphism (and don't even need you to have classes around!-) -- it's just (main use!) that often you have "rarely needed" arguments for choices that are generally made in a certain way, but it might be useful for the caller to set differently. 可选ARGS有一点做与多态性(和甚至不需要你有大约班- !) -它只是往往你已经“很少需要”的论点对于在一定通常所作的选择(主要使用!)方式,但对调用方进行不同的设置可能会很有用。

For example, consider built-in open . 例如,考虑内置open Most often , you open text files rather than binary opens, you open them for reading rather than for writing, and you're happy with the default buffering -- so, you just open('thefile.txt') and live happily ever after. 通常 ,您打开文本文件而不是打开二进制文件,打开它们以读取而不是写入,并且您对默认缓冲感到满意-因此,您只需open('thefile.txt')并在此之后幸福地生活。 Being able to specify the way in which you want to open (binary, for overwrite, for append, ...) as the second (optional) argument, instead of its default 'r' value, is of course often useful. 能够指定要打开的方式 (二进制,用于覆盖,可以进行追加,...)作为第二个(可选)参数,而不是其默认的'r'值,当然通常是有用的。 Once in a blue moon you want a file object with peculiar buffering options, and then having the buffering as the third (optional) argument (with a default value of course) pays big dividends... without it being in your way for the vast majority of the files you open! 一旦变成了蓝月亮,您想要一个具有特殊缓冲选项的文件对象, 然后将缓冲作为第三个(可选)参数(当然具有默认值)会带来巨大的收益……而不会妨碍您的工作您打开的大多数文件!

Inheritance and polymorphism would not really help one bit in getting the convenience of with open('blah.txt') as f: so concisely, while still allowing the same built-in function to serve many more use cases (eg, when you need to open a file for binary append without any buffering... maybe once a year if you code a lot;-). 继承和多态性并不能真正帮助您with open('blah.txt') as f:那样方便地with open('blah.txt') as f: ,同时仍允许相同的内置函数服务于更多用例(例如,当您需要打开文件以进行二进制追加而没有任何缓冲...如果您编写大量代码,则可能每年一次;-)。 And of course the convenience principles that apply to such built-in functions apply to the functions you write just as well!-) 当然,适用于此类内置函数的便利性原则也同样适用于编写的函数!-)

Optional arguments in Python serve multiple purposes, but most often they are a mechanism to provide defaults where there are sensible and infrequently varied values exist. Python中的可选参数有多种用途,但大多数情况下,它们是一种在存在合理且不经常变化的值的情况下提供默认值的机制。 Eg: 例如:

def open_http_connection(url, port=80, timeout=2):
  #...

A subtle variation is when multiple behaviors for a method are required based on the arguments provided, often using arity (number of arguments) or keyword arguments. 一种细微的变化是,当基于提供的参数(通常使用Arity(参数数量))或关键字参数需要一种方法的多种行为时。

# Example of arity based optional arguments
def do_something(*args):
  if not args:
    do_something1()
  elif len(args)==1:
    do_something2(args[0])
  else:
    do_something3(*args)

It may be helpful to study how variable positional and keyword arguments are specified in python: here . 研究如何在python中指定变量的位置和关键字参数可能会有所帮助: here

These methods for specifying optional and variable numbers of arguments are not as semantically complex as method overloading in statically typed object-oriented languages or various forms of multiple-dispatch as found in functional programming languages. 这些用于指定参数的可选数目和可变数目的方法在语义上不像在静态类型的面向对象的语言中重载的方法那样复杂,也不像在函数式编程语言中发现的多种形式的多调度那样复杂。 Python uses dynamic typing (sometimes called duck typing ), so these forms of dispatch are not idiomatic or terribly useful. Python使用动态类型化(有时称为鸭子类型 ),因此这些分派形式不是惯用的或非常有用的。 (This is not often seen as a limitation or disadvantage, though Python is certainly flexible enough to support multi-methods if one must have them.) (尽管Python当然足够灵活以支持多方法(如果必须要有多方法) ,但是这通常不被视为限制或缺点。)

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

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