嵌套函数范围问题 [英] Nested function scope problem

查看:59
本文介绍了嵌套函数范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我在

a函数中一遍又一遍地重复相同的几行,并决定将这些行拆分成嵌套函数

复制太多之后全面的微小变化。唯一的问题是

我的小助手功能不起作用!它声称变量

不存在。如果我移动变量声明,它会找到

变量,但不能改变它。在

嵌套函数中声明变量global也不起作用。


但是,更改包含范围中的变量是完整的目的/>
这个辅助函数。


我是python的新手,所以可能有一些解决方案我没有

遇到了。你能建议一个干净的解决方案吗?

违规代码如下。谢谢。


def breakLine(s):

"""将字符串分解为单词和符号列表。

""

def addTok():

if len(tok)0:

ls.append(tok) )

tok =''''


ls = []

tok =''''

splitters =''?()& |:〜,''

whitespace =''\\\\\\'

$对于c中的c,b $ b:

如果c分裂器:

addTok()

ls.append(c)

elif c in whitespace:

addTok()

else:

tok = tok + c

addTok()


返回ls


#some测试以确保其正常工作

print breakLine(''carolina(Prada):cat(X,Y)'')

print breakLine(''trouble:bird(X)& cat(Y)'')

print breakLine(''?trouble'')

I found that I was repeating the same couple of lines over and over in
a function and decided to split those lines into a nested function
after copying one too many minor changes all over. The only problem is
that my little helper function doesn''t work! It claims that a variable
doesn''t exist. If I move the variable declaration, it finds the
variable, but can''t change it. Declaring the variable global in the
nested function doesn''t work either.

But, changing the variable in the containing scope is the whole purpose
of this helper function.

I''m new to python, so there is probably some solution I haven''t
encountered yet. Could you please suggest a nice clean solution? The
offending code is below. Thanks.

def breakLine(s):
"""Break a string into a list of words and symbols.
"""
def addTok():
if len(tok) 0:
ls.append(tok)
tok = ''''

ls = []
tok = ''''
splitters = ''?()&|:~,''
whitespace = '' \t\n\r''

for c in s:
if c in splitters:
addTok()
ls.append(c)
elif c in whitespace:
addTok()
else:
tok = tok + c

addTok()

return ls

#some tests to make sure it works
print breakLine(''carolina(Prada):cat(X,Y)'')
print breakLine(''trouble :bird (X ) &cat ( Y )'')
print breakLine(''?trouble'')

推荐答案

2006-07-21 21:05: 22,Josiah Manson写道:
On 2006-07-21 21:05:22, Josiah Manson wrote:

我发现我在

a函数中一遍又一遍地重复相同的几行,并决定拆分这些行成为嵌套函数

后复制了一个太多的小改动。唯一的问题是

我的小助手功能不起作用!它声称变量

不存在。如果我移动变量声明,它会找到

变量,但不能改变它。在

嵌套函数中声明变量global也不起作用。


但是,更改包含范围中的变量是完整的目的/>
这个辅助函数。


我是python的新手,所以可能有一些解决方案我没有

遇到了。你能建议一个干净的解决方案吗?

违规代码如下。谢谢。
I found that I was repeating the same couple of lines over and over in
a function and decided to split those lines into a nested function
after copying one too many minor changes all over. The only problem is
that my little helper function doesn''t work! It claims that a variable
doesn''t exist. If I move the variable declaration, it finds the
variable, but can''t change it. Declaring the variable global in the
nested function doesn''t work either.

But, changing the variable in the containing scope is the whole purpose
of this helper function.

I''m new to python, so there is probably some solution I haven''t
encountered yet. Could you please suggest a nice clean solution? The
offending code is below. Thanks.



我不是Python专家,所以这里只是一些猜测......我不知道怎么回事

在内部函数内部创建变量。似乎只使用

名称会覆盖外部名称。似乎局部变量

在某种字典中;所以也许你可以通过那个

以某种方式访问​​它们。


另一个解决方案(有点难看)就是让这个类有两个

静态方法(breakLine和_addTok)和两个静态属性(_ls和

_tok)。


还有一个(也很难看)将是将tok和ls传递给addTok()和

再次传回tok。 (我认为不需要传回来,

,因为它是一个列表,所以它的数据被修改了.tok'的数据没有得到

修改,所以本地更改不会传播到外面。)


Gerhard

I''m no Python specialist, so here''s just some guesses... I don''t know how
to make variables known inside the inner function. It seems just using the
names there overrides the outside names. It also seems that local variables
are in some kind of dictionary; so maybe you can access them through that
somehow.

One other solution (a bit ugly) would be to make this a class with two
static methods (breakLine and _addTok) and two static attributes (_ls and
_tok).

Still another (also ugly) would be to pass both tok and ls to addTok() and
pass tok back out again. (I think ls doesn''t have to be passed back,
because it is a list and so its data gets modified. tok''s data doesn''t get
modified, so local changes don''t propagate to the outside.)

Gerhard


Gerhard Fiedler写道:
Gerhard Fiedler wrote:

2006-07-21 21:05:22,Josiah Manson写道:
On 2006-07-21 21:05:22, Josiah Manson wrote:

我发现我在

a函数中一遍又一遍地重复相同的几行,并决定将这些行拆分成嵌套函数

复制了一个太多的小改动遍。唯一的问题是

我的小助手功能不起作用!它声称变量

不存在。如果我移动变量声明,它会找到

变量,但不能改变它。在

嵌套函数中声明变量global也不起作用。


但是,更改包含范围中的变量是完整的目的/>
这个辅助函数。


我是python的新手,所以可能有一些解决方案我没有

遇到了。你能建议一个干净的解决方案吗?

违规代码如下。谢谢。
I found that I was repeating the same couple of lines over and over in
a function and decided to split those lines into a nested function
after copying one too many minor changes all over. The only problem is
that my little helper function doesn''t work! It claims that a variable
doesn''t exist. If I move the variable declaration, it finds the
variable, but can''t change it. Declaring the variable global in the
nested function doesn''t work either.

But, changing the variable in the containing scope is the whole purpose
of this helper function.

I''m new to python, so there is probably some solution I haven''t
encountered yet. Could you please suggest a nice clean solution? The
offending code is below. Thanks.



我不是Python专家,所以这里只是一些猜测......我不知道如何

在内部函数内部创建变量。似乎只使用

名称会覆盖外部名称。似乎局部变量

在某种字典中;所以也许你可以通过那个

以某种方式访问​​它们。


另一个解决方案(有点难看)就是让这个类有两个

静态方法(breakLine和_addTok)和两个静态属性(_ls和

_tok)。


还有一个(也很难看)将是将tok和ls传递给addTok()和

再次传回tok。 (我认为不需要传回来,

,因为它是一个列表,所以它的数据被修改了.tok'的数据没有得到

已修改,因此本地更改不会传播到外部。)


Gerhard


I''m no Python specialist, so here''s just some guesses... I don''t know how
to make variables known inside the inner function. It seems just using the
names there overrides the outside names. It also seems that local variables
are in some kind of dictionary; so maybe you can access them through that
somehow.

One other solution (a bit ugly) would be to make this a class with two
static methods (breakLine and _addTok) and two static attributes (_ls and
_tok).

Still another (also ugly) would be to pass both tok and ls to addTok() and
pass tok back out again. (I think ls doesn''t have to be passed back,
because it is a list and so its data gets modified. tok''s data doesn''t get
modified, so local changes don''t propagate to the outside.)

Gerhard



第三个选项似乎工作正常。


def breakLine(s):

"""将字符串分成单词和符号列表。

"""


def addTok(tok,ls):

if len(tok)0:

ls.append(tok)

tok =''''

返回tok


ls = []

tok =''''

splitters =''?()& |:〜,''

whitespace =''\\ \\ t \\\\''


for c in s:

if c in splitters:

tok = addTok(tok,ls)

ls.append(c)

elif c in whitespace:

tok = addTok(tok,ls)

else:

tok = tok + c

tok = addTok(tok,ls)


返回ls


#some测试以确保其正常工作

print breakLine(''carolina(Prada):cat(X,Y)'')

print breakLine(''trouble:bird(X)& cat(Y)' ')

打印breakLine(''?麻烦'')


#打印:

[''carolina'', ''('',''普拉达'',''''','':'',''猫'',''('',''X'','','',''' Y'','')'']

[''麻烦'','':'',''鸟'',''('',''X'',' ')'',''&'',''cat'',''('',''Y'','')'']

[''?''' ,''麻烦'']

That third option seems to work fine.

def breakLine(s):
"""Break a string into a list of words and symbols.
"""

def addTok(tok, ls):
if len(tok) 0:
ls.append(tok)
tok = ''''
return tok

ls = []
tok = ''''
splitters = ''?()&|:~,''
whitespace = '' \t\n\r''

for c in s:
if c in splitters:
tok = addTok(tok, ls)
ls.append(c)
elif c in whitespace:
tok = addTok(tok, ls)
else:
tok = tok + c

tok = addTok(tok, ls)

return ls

#some tests to make sure it works
print breakLine(''carolina(Prada):cat(X,Y)'')
print breakLine(''trouble :bird (X ) &cat ( Y )'')
print breakLine(''?trouble'')

# Prints:
[''carolina'', ''('', ''Prada'', '')'', '':'', ''cat'', ''('', ''X'', '','', ''Y'', '')'']
[''trouble'', '':'', ''bird'', ''('', ''X'', '')'', ''&'', ''cat'', ''('', ''Y'', '')'']
[''?'', ''trouble'']


Simon Forman写道:
Simon Forman wrote:

第三个选项似乎工作正常。
That third option seems to work fine.



确实如此,但它仍然有很多问题


如果len(tok)0:

应该写成

if(tok):


tok =''''

tok = toc + c

应该写成

tok = []

tok.append(c)

然后

''''。join(toc)


无论如何,整个东西应该用这样的东西代替:

import re

def breakLine(s):

splitters =''?()& |:〜,''

chars =''^ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ (?:[%s])

|

(?:[%s] +))''''''%(分离器,字符)

返回re.findall(正则表达式,s,re.VERBOSE)


如果要使用
$,应该可以更简化b $ b内置于正则表达式标准中的字符列表。


-

- 贾斯汀

Well it does, but there are still many things wrong with it

if len(tok) 0:
should be written as
if(tok):

tok = ''''
tok = toc + c
should be written as
tok = []
tok.append(c)
and later
''''.join(toc)

anyway, the entire thing should be replaced with something like this:
import re
def breakLine(s):
splitters = ''?()&|:~,''
chars = ''^ \t\n\r\f\v%s'' % splitters
regex = ''''''(
(?:[%s])
|
(?:[%s]+))'''''' % (splitters, chars)
return re.findall(regex, s,re.VERBOSE)

That should be able to be simplified even more if one were to use the
character lists built into the regex standard.

--
- Justin


这篇关于嵌套函数范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆