argparse module How to add option without any argument?

I have created a script using argparse . The script needs to take a configuration file name as an option, and user can specify whether they need to proceed totally the script or only simulate it. The args to be passed: ./script -f config_file -s or ./script -f config_file . It's ok for the -f config_file part, but It keeps asking me for arguments for the -s which is optionnal and should

argparse模块如何添加没有任何参数的选项?

我用argparse创建了一个脚本。 该脚本需要将配置文件名称作为选项,用户可以指定是完全执行脚本还是仅对其进行模拟。 要传递的参数: ./script -f config_file -s或./script -f config_file 。 这对于-f config_file部分是可以的,但是它不断询问我是否是选项的参数,并且不应该跟随任何参数。 我试过这个: parser = argparse.ArgumentParser() parser.add_argument('-f', '--file') #parser.add_argument('-s', '--sim

Mutable Default Method Arguments In Python

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I am using the Python IDE PyCharm and something that is have by default is it will showing warning when I have a mutbale type as a default value. For example, when I have this: def status(self, options=[]): PyCharm wants it to looks like: def status(self, options=None): if not options: options = [] My qu

Python中的可变默认方法参数

可能重复: Python中的“最小惊讶”:可变的默认参数 我使用的是Python IDE PyCharm,默认情况下,它有一个默认值为mutbale类型时会显示警告。 例如,当我有这个: def status(self, options=[]): PyCharm希望它看起来像: def status(self, options=None): if not options: options = [] 我的问题是这是否是在Python社区中做事的标准方式,还是PyCharm认为它应该完成的方式? 将可变数据类型作为默认方法参数存在缺

Mutable default arguments in Python

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I have written the following python program: #!/usr/bin/env python def bug( numbers = [] ): numbers.append( 1 ) return numbers print bug() print bug() The result i would expect is [1] [1] But i got [1] [1, 1] Is this a bug? No, this is not a bug and this behaviour has been around in Python for a

Python中的可变默认参数

可能重复: Python中的“最小惊讶”:可变的默认参数 我写了下面的python程序: #!/usr/bin/env python def bug( numbers = [] ): numbers.append( 1 ) return numbers print bug() print bug() 我期望的结果是 [1] [1] 但我得到了 [1] [1, 1] 这是一个错误? 不,这不是一个bug,并且这种行为在Python中已经存在很长时间了。 问题是列表对象是可变的,即你可以改变它,当你调用一个函数时,你不会得到一

Pass a list to a class python

This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 30 answers This looks like it's happening because you're passing the same list to every object. As a result, all the objects maintain references to the same list, and since list is mutable, it appears to change "all" of them at once. To fix this, either pass in a new empty list

将一个列表传递给一个类python

这个问题在这里已经有了答案: “最小的惊讶”和可变的默认参数30的答案 这看起来像是发生了,因为你将相同的列表传递给每个对象。 因此,所有对象都保持对同一列表的引用,并且由于list是可变的,它似乎一次改变它们的“全部”。 要解决此问题,请在每次创建revs对象时传入新的空列表,否则请克隆传入的列表: cada = revs(rev, us, list_acct[:]) 请注意,如果list_acct包含可变对象,则仍然可能再次遇到同样的问题,但更

Lifetime of default function arguments in python

This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 30 answers As the argument is an attribute of the function object, it normally has the same lifetime as the function. Usually, functions exist from the moment their module is loaded, until the interpreter exits. However, Python functions are first-class objects and you can delete all references

在Python中默认函数参数的生命周期

这个问题在这里已经有了答案: “最小的惊讶”和可变的默认参数30的答案 由于参数是函数对象的一个​​属性,它通常具有与该函数相同的生命周期。 通常,函数从模块加载的那一刻起,直到解释器退出。 但是,Python函数是第一类对象,您可以尽早删除所有对该函数的引用(动态)。 垃圾收集器然后可以收获该函数并随后获得默认参数: >>> def foo(bar, spam=[]): ... spam.append(bar) ... print(spam) ...

Python: Unexpected behavior using contextmanager on class method

This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 30 answers Python constructor and default value [duplicate] 4 answers This behavior is due to how mutable default arguments work in Python. Try changing SymList.__init__() to the following: def __init__(self, L=None): if L is None: self.L = [] else:

Python:在类方法上使用contextmanager的意外行为

这个问题在这里已经有了答案: “最小的惊讶”和可变的默认参数30的答案 Python的构造函数和默认值[复制] 4个答案 这种行为是由于默认参数在Python中的工作原理。 尝试将SymList.__init__()更改为以下内容: def __init__(self, L=None): if L is None: self.L = [] else: self.L = L 当您在一个实例中修改self.L ,您还正在修改传入SymList.__init__()的L ,因此使用您

The value of an empty list in function parameter, example here

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument def f(a, L=[]): L.append(a) return L print(f(1, [1, 2])) print(f(1)) print(f(2)) print(f(3)) I wonder why the other f(1), f(2), f(3) has not append to the first f(1, [1,2]). I guess the result should be : [1, 2, 1] [1, 2, 1, 1] [1, 2, 1, 1, 2] [1, 2, 1, 1, 2, 3] But the result is not this. I do not k

函数参数中一个空列表的值,例如这里

可能重复: Python中的“最小惊讶”:可变的默认参数 def f(a, L=[]): L.append(a) return L print(f(1, [1, 2])) print(f(1)) print(f(2)) print(f(3)) 我想知道为什么其他f(1),f(2),f(3)没有附加到第一个f(1,[1,2])。 我想结果应该是: [1, 2, 1] [1, 2, 1, 1] [1, 2, 1, 1, 2] [1, 2, 1, 1, 2, 3] 但结果不是这样。 我不知道为什么。 有两个不同的问题(更好地称为概念)融合成一个问题陈述。

Python base classes share attributes?

This question already has an answer here: Python constructor and default value [duplicate] 4 answers “Least Astonishment” and the Mutable Default Argument 30 answers You're making a common Python newcomer mistake. See my answer here: How should I declare default values for instance variables in Python? Briefly explained, Python interprets the class definitions only once . That mean

Python基类共享属性?

这个问题在这里已经有了答案: Python的构造函数和默认值[复制] 4个答案 “最小的惊讶”和可变的默认参数30的答案 你正在犯一个普通的Python新手错误。 在这里看到我的答案:我应该如何在Python中声明实例变量的默认值? 简而言之,Python只解释类定义一次 。 这意味着在__init__()方法中声明的所有内容只会被创建一次。 或者,换句话说,你的[]列表默认参数只有一次。 然后self.l = l在每次创建一个新类时为同一个

Python: Dictionary as instance variable

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about the behavior of dictionaries as class instance variables in Python 3. The way I understand it, instance variables in Python have per-instance storage, unlike class variables which are per-class (similar to what some other languages call "static"). And this seems to hold tru

Python:Dictionary作为实例变量

可能重复: Python中的“最小惊讶”:可变的默认参数 我对Python 3中字典的行为类型实例变量非常困惑。根据我的理解,Python中的实例变量具有每个实例的存储空间,与每个类的类变量不同(类似于某些其他语言所称的“静态的”)。 这似乎成立,除非实例变量是从默认参数创建的字典。 例如: class Foo: def __init__(self, values = dict()): self.values = values f1 = Foo() f1.values["hello"] = "world" f2

Default value in a function in Python

This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 30 answers Yes, it's correct behavior. However, from your question, it appears that it's not what you expected. If you want it to match your expectations, be aware of the following: Rule 1. Do not use mutable objects as default values. def anyFunction( arg=[] ): Will not create a fr

Python中函数的默认值

这个问题在这里已经有了答案: “最小的惊讶”和可变的默认参数30的答案 是的,这是正确的行为。 然而,从你的问题来看,这似乎并不是你所期望的。 如果您希望它符合您的期望,请注意以下事项: 规则1.不要使用可变对象作为默认值。 def anyFunction( arg=[] ): 不会创建一个新的列表对象。 arg的默认列表对象将在整个地方共享。 同样 def anyFunction( arg={} ): 将不会创建一个新的字典对象。 这个默认字典将