python what happens when a function is called?

I am using pdb to debug a program. I successively hit 'c' to run through the code and at each step pdb shows me which line is executed. Let's say we have this code: def foo(bar): print(bar) foo('hey') First, line 4 calls function foo. Then pdb shows me the line def foo(bar) is executed. Why? Is not that line just a kind of label? What happens before "print(bar)&qu

python当函数被调用时会发生什么?

我正在使用pdb来调试程序。 我连续击中'c'来运行代码,并且在每个步骤中,pdb显示执行哪条线。 假设我们有这样的代码: def foo(bar): print(bar) foo('hey') 首先,第4行调用函数foo。 然后pdb显示我的线 def foo(bar) 被执行。 为什么? 那不就是一种标签吗? “print(bar)”执行之前会发生什么? (随着另一个'命中) 编辑:我尝试做的事情是实际检查定义。 事实上,在foo是一个生成器(不能

How to make Python Wait for input?

I am using python in Maya, a 3D animation package. I would loved to run a definition (A) but within that definition I want another definiton (B) that requires a valid object selection. The script will kept going until one is made (in def B) and I want to continue with my script (def A) with a returned value from def B. How can I tell def A to wait until a valid returned value is retreived from

如何让Python等待输入?

我在Maya中使用python,一个3D动画包。 我喜欢运行一个定义(A),但是在这个定义中,我需要另一个定义(B),它需要有效的对象选择。 这个脚本会一直持续下去直到创建一个(在def B中),并且我想用def B的返回值继续我的脚本(def A)。我如何告诉def A等待直到返回有效的返回值def B? 这么简单的问题:我如何让python等待一个有效的返回值被接收? 我希望这是有道理的,并提前感谢您的时间。 C 例: def commandA

Default values for arguments

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function: def foo(L = []): L.append(1) print L Each time I call foo it will print a new list with more elements than previous time, eg: >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1] Now consider the following function: def goo(a = 0): a += 1 prin

参数的默认值

可能重复: Python中的“最小惊讶”:可变的默认参数 考虑以下功能: def foo(L = []): L.append(1) print L 每次我调用foo时,它都会打印一个比以前更多的元素的列表,例如: >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1] 现在考虑以下功能: def goo(a = 0): a += 1 print a 当多次调用它时,我们得到以下图片: >>> goo() 1 >>> goo() 1 >>>

gotchas to be aware of

While investigating scoping in Perl and Python , I came across a silent scoping related behavior of Perl that can cause bugs very difficult to trace. Specifically for programmers who are new to the language and not fully aware of all it's nuances. I have provided example code for both Perl and Python to illustrate how scoping works in both the languages In Python if we run the code:

需要注意的问题

在调查Perl和Python的范围界定时 ,我遇到了一个与Perl无关的与范围有关的行为,可能会导致很难跟踪的bug。 特别针对那些刚接触这门语言的程序员,并没有完全意识到它的细微差别。 我已经提供了Perl和Python的示例代码,以说明范围在两种语言中的工作原理 在Python中,如果我们运行代码: x = 30 def g(): s1 = x print "Inside g(): Value of x is %d" % s1 def t(var): x = var

Get the class name of a decorated class method

Consider this scenario: #!/usr/bin/env python # -*- coding: utf-8 -*- import functools def wrapmethod(f): @functools.wraps(f) def wrap(*args, **kwargs): print '>> %s' % (f.func_name) # Here I'll do pre-processing r = f(*args, **kwargs) # Here I'll do post-processing return r return wrap @wrapmethod def foo(): pass class Test(object):

获取装饰类方法的类名

考虑这种情况: #!/usr/bin/env python # -*- coding: utf-8 -*- import functools def wrapmethod(f): @functools.wraps(f) def wrap(*args, **kwargs): print '>> %s' % (f.func_name) # Here I'll do pre-processing r = f(*args, **kwargs) # Here I'll do post-processing return r return wrap @wrapmethod def foo(): pass class Test(object): @wrap

Argparse optional positional arguments?

I have a script which is meant to be used like this: usage: installer.py dir [-h] [-v] dir is a positional argument which is defined like this: parser.add_argument('dir', default=os.getcwd()) I want the dir to be optional: when it's not specified it should just be cwd . Unfortunately when I don't specify the dir argument, I get Error: Too few arguments . Use nargs='?' (or

Argparse可选的位置参数?

我有一个脚本,它是这样使用的: usage: installer.py dir [-h] [-v] dir是一个定义如下的位置参数: parser.add_argument('dir', default=os.getcwd()) 我希望dir是可选的:当它没有被指定时,它应该是cwd 。 不幸的是,当我不指定dir参数时,我得到Error: Too few arguments 。 使用nargs='?' (或者如果你需要多个目录,那么nargs='*' ) parser.add_argument('dir', nargs='?', default=os.getcwd(

What are the implications of using mutable types as default arguments in Python?

Possible Duplicates: Why the “mutable default argument fix” syntax is so ugly, asks python newbie least astonishment in python: the mutable default argument Here is an example. def list_as_default(arg = []): pass From: http://www.network-theory.co.uk/docs/pytut/DefaultArgumentValues.html The default value is evaluated only once. This makes a difference when the default is a mutable

在Python中使用可变类型作为默认参数有什么含义?

可能重复: 为什么“可变默认参数修复”语法如此丑陋,请求python newbie 最不惊讶的是python:可变的默认参数 这是一个例子。 def list_as_default(arg = []): pass 来自:http://www.network-theory.co.uk/docs/pytut/DefaultArgumentValues.html 默认值只计算一次。 当默认值是可变对象(如列表,字典或大多数类的实例)时,这会有所不同。 例如,以下函数会累积在后续调用中传递给它的参数: def f(a, L=[]):

Scoping in Python 'for' loops

I'm not asking about Python's scoping rules; I understand generally how scoping works in Python for loops. My question is why the design decisions were made in this way. For example (no pun intended): for foo in xrange(10): bar = 2 print(foo, bar) The above will print (9,2). This strikes me as weird: 'foo' is really just controlling the loop, and 'bar' was defin

在Python中为'循环'确定范围

我不是在问Python的范围规则, 我通常理解Python的循环范围是如何工作的。 我的问题是为什么这样做的设计决定。 例如(没有双关意图): for foo in xrange(10): bar = 2 print(foo, bar) 以上将打印(9,2)。 这让我感到奇怪:'foo'实际上只是控制循环,'bar'是在循环内定义的。 我可以理解为什么可能有必要在循环外部访问'bar'(否则,for循环将具有非常有限的功能)。 我不明白的是,为

Good uses for mutable function argument default values?

It is a common mistake to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger: >>> def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list >>> print bad_append('one') ['one'] >>> print bad_append('two') ['one', 'two'] The explanation why th

可变函数参数默认值的良好用法?

将可变对象设置为函数中参数的默认值是常见的错误。 以下是David Goodger撰写的优秀文章的一个例子: >>> def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list >>> print bad_append('one') ['one'] >>> print bad_append('two') ['one', 'two'] 这种情况发生的原因就在这里。 现在对于我的问题: 这种语法是否有很好的用例? 我的意思是,如果每个

When I am in the Python or IPython console, what is called when I am returned an output?

For example, python >> x = 1 >> x 1 I'm curious about what method/function on x is returning 1. I'm asking because I'm seeing differences between calling print x and simply x . Similary, is there a way to specify what is called? Does this configuration exist in IPython? When you inspect an object in that manner in a REPL, it invokes the object's __repr__ method.

当我在Python或IPython控制台中时,我返回输出时调用了什么?

例如, python >> x = 1 >> x 1 我很好奇x上的方法/函数返回1.我在问,因为我看到调用print x和简单的x之间的区别。 相似,有没有办法指定所谓的? 这个配置是否存在于IPython中? 当您在REPL中以这种方式检查对象时,它会调用该对象的__repr__方法。 相比之下, print使用对象的__str__方法。 例: >>> class Widget: ... def __repr__(self): ... return "repr of a Widget" ..