Python 访问函数外部的函数变量,而不使用;“全球”;
我试图在Python中访问函数外部的局部函数变量。那么比如说,Python 访问函数外部的函数变量,而不使用;“全球”;,python,local-variables,Python,Local Variables,我试图在Python中访问函数外部的局部函数变量。那么比如说, bye = '' def hi(): global bye something something bye = 5 sigh = 10 hi() print bye 上面的工作很好,因为它应该。因为我想知道我是否可以访问bye外部hi()而不使用global bye,所以我尝试了: def hi(): something something bye = 5
bye = ''
def hi():
global bye
something
something
bye = 5
sigh = 10
hi()
print bye
上面的工作很好,因为它应该。因为我想知道我是否可以访问bye
外部hi()
而不使用global bye
,所以我尝试了:
def hi():
something
something
bye = 5
sigh = 10
return
hi()
x = hi()
print x.bye
def hi():
something
something
bye = 5
sigh = 10
return bye
hi()
x = hi()
print x.bye
上面给出了AttributeError:“NoneType”对象没有属性“bye”
然后,我试着:
def hi():
something
something
bye = 5
sigh = 10
return
hi()
x = hi()
print x.bye
def hi():
something
something
bye = 5
sigh = 10
return bye
hi()
x = hi()
print x.bye
这次它甚至没有给出一个错误
那么,有没有一种方法可以访问函数(
hi()
)外部的局部函数变量(bye
),而不使用全局变量,也不打印变量sigh
?(问题被编辑为在@hcwhsa下面的评论之后包含sigh
。问题是在将x设置为字符串后调用print x.bye。当运行x=hi()
时,它运行hi()并将x的值设置为5(bye的值;它没有将x的值设置为bye变量本身的引用).EX:bye=5;x=bye;bye=4;打印x;
打印5,而不是4
此外,您不必运行hi()两次,只需运行x=hi()
,而不是hi();x=hi()
(就像您运行hi()的方式一样,不使用结果值5执行任何操作,然后重新运行相同的hi(),并将5的值保存到x变量
所以完整的代码应该是
def hi():
something
something
bye = 5
return bye
x = hi()
print x
如果您想返回多个变量,一个选项是使用列表或字典,这取决于您需要什么
例:
关于python字典的更多信息,请访问,您可以按照以下思路做一些事情(当我测试python v2.7.17和v3.8.1时,这两个版本都适用): 函数是Python中的对象,可以为其指定任意属性 如果要经常做这种事情,可以通过创建一个函数装饰器来实现更通用的功能,该装饰器在对装饰函数的每次调用中添加一个
this
参数
此附加参数将为函数提供一种无需显式嵌入(硬代码)即可引用自身的方法它们的名称包含在定义的其余部分,类似于类方法自动接收的实例参数,作为它们的第一个参数,通常命名为self
——我选择了一些不同的名称以避免混淆,但与self
参数一样,它可以根据您的意愿命名
以下是该方法的一个示例:
def add_this_arg(func):
def wrapped(*args, **kwargs):
return func(wrapped, *args, **kwargs)
return wrapped
@add_this_arg
def hi(this, that):
# other code...
this.bye = 2 * that # Create function attribute.
sigh = 10
hi(21)
print(hi.bye) # -> 42
注
这对类方法不起作用。只需使用按约定名为
self
的实例参数,该参数已传递给方法而不是方法的名称。您可以通过type(self)
引用类级属性。请参阅。您可以执行以下操作:
def static_example():
if not hasattr(static_example, "static_var"):
static_example.static_var = 0
static_example.static_var += 1
return static_example.static_var
print static_example()
print static_example()
print static_example()
我也遇到过同样的问题。对您的问题的一个回答让我想到了以下想法(最终成功了)。我使用Python 3.7
# just an example
def func(): # define a function
func.y = 4 # here y is a local variable, which I want to access; func.y defines
# a method for my example function which will allow me to access
# function's local variable y
x = func.y + 8 # this is the main task for the function: what it should do
return x
func() # now I'm calling the function
a = func.y # I put it's local variable into my new variable
print(a) # and print my new variable
然后我在Windows PowerShell中启动这个程序,得到答案4。
结论:为了能够访问局部函数的变量,可以在局部变量的名称前添加函数名和一个点(然后,当然,使用此结构在函数体内外调用变量)。
我希望这会有所帮助。
bye=hi();print bye
您想实现什么?用例是什么?谢谢@hcwhsa!这很有效。后续问题。假设函数定义了多个变量,但我只想打印bye
变量。但是,此解决方案将同时打印这两个变量。我将相应地编辑我的问题。您真的对学习如何打印感兴趣吗使用return
或其他方法?如果您使用函数计算值,然后从函数外部使用该值,那么您最好直接返回计算值。根据您显示的代码,您不需要静态变量,也不需要通过函数itsel访问值f、 你只是想弄清楚如何给调用者你计算的值。返回值就是你要做的。请看并让我知道。谢谢。这很有帮助。尽管我无法摆脱这样的感觉,即有一个干净的/有文档记录的技巧可以实现它-定义多个局部变量,然后在函数外只调用其中一个。你就是我们lcome,但我必须补充一点,这种做法是相当不寻常的。可能很难调试,并且使代码很难维护——因为本质上它与使用全局变量是一样的,而全局变量已经存在很长时间了。如果以这种方式使用它,这是行不通的。if hi():print hi.bye
。有人知道为什么吗?@erm3nda:根据定义,hi()
函数没有返回值,因此它有效地返回None
,这被认为是False
值,因此if
不执行条件print
语句。@ReeshabhRanjan:是的。print hi.bye
需要更改为print(hi.bye)
由于Python 2和3之间的差异,请参见文档。由于在这种情况下,较新的语法在两个版本中都适用,因此我将更新我的答案以避免让人困惑…@ReeshabhRanjan:正如我之前所说的,至少对我来说,使用Python 3.6.3是可行的(没有理由不这样做)-我只是再次运行它来确认。如果它对您不起作用,那么一定是出了其他问题。请注意,在尝试访问该属性之前,您必须至少调用该函数一次,才能创建它。
# just an example
def func(): # define a function
func.y = 4 # here y is a local variable, which I want to access; func.y defines
# a method for my example function which will allow me to access
# function's local variable y
x = func.y + 8 # this is the main task for the function: what it should do
return x
func() # now I'm calling the function
a = func.y # I put it's local variable into my new variable
print(a) # and print my new variable