Short Description of the Scoping Rules?

What exactly are the Python scoping rules? If I have some code: code1 class Foo: code2 def spam..... code3 for code4..: code5 x() Where is x found? Some possible choices include the list above: In the enclosing source file In the class namespace In the function definition In the for loop index variable Inside the for loop Also there is the context

范围规则的简短描述?

Python范围规则究竟是什么? 如果我有一些代码: code1 class Foo: code2 def spam..... code3 for code4..: code5 x() x在哪里找到? 一些可能的选择包括上面的列表: 在封闭的源文件中 在类名称空间中 在函数定义中 在for循环索引变量中 在for循环中 在执行过程中还有上下文,当功能垃圾邮件被传递到其他地方时。 也许lambda函数有点不同? 某处必须有一个简单的参考或算

python decorator for class methods

I have a decorator to register some class methods. How can I get both self and run parameters correctly? class Task(object): _tasks = [] @staticmethod def register(name): def decorator(fn): @wraps(fn) def wrapper(self=None, run=True, *args, **kwargs): if not run: task = defaultdict() task['name

类方法的python装饰器

我有一个装饰器来注册一些类的方法。 我怎样才能正确地获得self和run参数? class Task(object): _tasks = [] @staticmethod def register(name): def decorator(fn): @wraps(fn) def wrapper(self=None, run=True, *args, **kwargs): if not run: task = defaultdict() task['name'] = name task

python, confused in decorate and closure

I have some test code: def num(num): def deco(func): def wrap(*args, **kwargs): inputed_num = num return func(*args, **kwargs) return wrap return deco @num(5) def test(a): return a + inputed_num print test(1) when run this code, I got an error shows that 'inputed_num' is not defined My question is: In wrap function, is there not

python,在装饰和关闭时感到困惑

我有一些测试代码: def num(num): def deco(func): def wrap(*args, **kwargs): inputed_num = num return func(*args, **kwargs) return wrap return deco @num(5) def test(a): return a + inputed_num print test(1) 当运行这段代码时,我得到一个错误,表明'inputed_num'没有被定义 我的问题是:在包装函数中,是否没有func可以得到''inputed

argparse: change argument from positional to optional in certain cases

My script has the following usage: prog.py login password [--show] login and password are positional arguments. However, I want to transform them into an optional if another argument ( -s or --show ) is specified. So, the usage will be: prog.py --show [login] [password] This is my code: parser = argparse.ArgumentParser(...) parser.add_argument('login', help="User login") parser.add_argumen

argparse:在某些情况下将参数从位置更改为可选

我的脚本有以下用法: prog.py login password [--show] login和password是位置参数。 但是,如果指定了另一个参数( -s或--show ),我想将它们转换为可选项。 所以,用法是: prog.py --show [login] [password] 这是我的代码: parser = argparse.ArgumentParser(...) parser.add_argument('login', help="User login") parser.add_argument('password', help="User password") parser.add_argument('-s', '--show', de

Argparse: Required arguments listed under "optional arguments"?

I use the following simple code to parse some arguments; note that one of them is required. Unfortunately, when the user runs the script without providing the argument, the displayed usage/help text does not indicate that there is a non-optional argument, which I find very confusing. How can I get python to indicate that an argument is not optional? Here is the code: import argparse if __na

Argparse:“可选参数”下列出的必需参数?

我使用下面的简单代码来解析一些参数; 请注意其中一个是必需的。 不幸的是,当用户在不提供参数的情况下运行脚本时,显示的用法/帮助文本并不表示存在非可选参数,这让我感到非常困惑。 我怎样才能让python指出参数不是可选的? 代码如下: import argparse if __name__ == '__main__': parser = argparse.ArgumentParser( description='Foo') parser.add_argument('-i','--input', help='Input file name'

argparse "compulsory" optional arguments

Python's argparse module has what are called 'optional' arguments. All arguments whose name starts with - or -- are optional by default. Typically, compulsory arguments are positional, and hence when running the program, they are not explicitly named. For example, in a script which had: parser.add_argument('language', help="Output language") Invocations would look like: $ hello

强制“强制性”可选参数

Python的argparse模块具有所谓的“可选”参数。 所有以-或--开头的参数默认是可选的。 通常,强制参数是位置的,因此在运行程序时,它们没有明确命名。 例如,在一个脚本中有: parser.add_argument('language', help="Output language") 调用将如下所示: $ hello-world czech 有时通过名称传递强制性参数可能会更好(例如脚本调用更容易以这种方式读取),但仍然是强制性的。 即 $ hello-world --use-lang czech 如何

argparse optional positional argument and subparsers arguments

I have a python script that takes in an optional positional argument and has a few subcommands. Some of these subcommands require the positional argument, some don't. The problem I have appears when I try to use a subcommand that does not require the positional argument. Consider the following test file: import argparse argp = argparse.ArgumentParser() argp.add_argument('inputfile', type

argparse可选的位置参数和子分析器参数

我有一个python脚本,它接受一个可选的位置参数,并有几个子命令。 其中一些子命令需要位置参数,有些则不需要。 我尝试使用不需要位置参数的子命令时出现了问题。 考虑以下测试文件: import argparse argp = argparse.ArgumentParser() argp.add_argument('inputfile', type=str, nargs='?', help='input file to process') argp.add_argument('--main_opt1', type=str, help='global

Python argparse: nargs='?' and optional arguments

Let's say I have a simple program called readfile.py which supports command line arguments by using Python's argparse . The program reads from a file specified by the positional argument input . Without specifying additional arguments, this program is not very exciting. It just reads from input and exits. The optional argument --output [OUTPUT_FILE] signifies that the input file shou

Python argparse:nargs ='?' 和可选参数

假设我有一个名为readfile.py的简单程序,它使用Python的argparse支持命令行参数。 该程序从位置参数input指定的文件读取。 没有指定更多的参数,这个程序并不是很令人兴奋。 它只是从input读取并退出。 可选参数--output [OUTPUT_FILE]表示输入文件应写入文件。 OUTPUT_FILE也是可选的。 如果未指定,则输入应写入default.out 。 我设置了我的参数解析器,如下所示: parser = argparse.ArgumentParser(description='R

Python argparse command line flags without arguments

How do I add an optional flag to my command line args? eg. so I can write python myprog.py or python myprog.py -w I tried parser.add_argument('-w') But I just get an error message saying Usage [-w W] error: argument -w: expected one argument which I take it means that it wants an argument value for the -w option. What's the way of just accepting a flag? I'm finding http://d

Python无需参数即可指定命令行标志

我如何添加一个可选的标志到我的命令行参数? 例如。 所以我可以写 python myprog.py 要么 python myprog.py -w 我试过了 parser.add_argument('-w') 但我只是收到一条错误消息 Usage [-w W] error: argument -w: expected one argument 我认为这意味着它需要-w选项的参数值。 接受国旗的方式是什么? 我在这个问题上发现http://docs.python.org/library/argparse.html而不透明。 就像你所拥有的那样,参数w在

Simple argparse example wanted: 1 argument, 3 results

The documentation for the argparse python module, while excellent I'm sure, is too much for my tiny beginner brain to grasp right now. I don't need to do math on the command line or meddle with formatting lines on the screen or change option characters. All I want to do is "If arg is A, do this, if B do that, if none of the above show help and quit". My understanding of the

简单的想法:1个参数,3个结果

argparse python模块的文档虽然非常出色,但对于我的小小初学者大脑来说,现在太多了。 我不需要在命令行上进行数学运算,也不需要在屏幕上插入格式化线或更改选项字符。 我想要做的就是“如果arg是A,那么做到这一点,如果B做到了,如果以上都没有显示帮助并退出”。 我对原始问题的理解有两方面。 首先,就最简单可能的考虑范例而言,我很惊讶我没有在这里看到它。 当然,简单来说,这也是所有开销都很小的权力,但它可能