覆盖函数内部的全局变量不适用于Spyder 4 [英] Override global variable inside function not working with Spyder 4

查看:140
本文介绍了覆盖函数内部的全局变量不适用于Spyder 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从函数中覆盖全局定义的数据框。
不知何故,全局值不会改变,在函数中打印数据框符合预期值。

I try to override globally defined Dataframes from within a function. Somehow the global values do not change, printing the dataframes within the functions works with the expected values.

import pandas as pd

rawData = pd.read_csv("music.csv")
appTitles =pd.DataFrame #also with pd.DataFrame() wont get affected

def loadTitles(df=rawData):
    global appTitles
    appTitles = pd.DataFrame({'Title' : df['title']})
    print(appTitles) 
return appTitles #(second 'print' to screen)

#RUN FILE
#CALL FUNCTION: loadTitles() #-> No Variable appTitles

变量资源管理器中的appTitles变量保持为空或不存在(取决于pd.DataFrame的使用) /pd.DataFrame())谢谢!

The appTitles variable in the variable explorer remains empty or nonexistent (depending use of pd.DataFrame/pd.DataFrame()) Thank you!

编辑: @ 4turkuaz评论说在他的环境上一切正常,我尝试了Python Windows cmd以及Spyder3和Python 3.7.4&在IPython 7.8.0上也一切正常。

After @4turkuaz commented that on his environment everthing works as expected, I tried Python console within Windows cmd and also on Spyder3 with Python 3.7.4 & IPython 7.8.0 where also everything worked fine.

仅在Spyder4和Python 3.7.5& IPython 7.10.1无法正常工作。也许是个错误?我不知道...

Only on Spyder4 with Python 3.7.5 & IPython 7.10.1 it won't work. Maybe a bug? I don't know...

编辑::将Spyder4降级为Python 3.7.4& IPython 7.8.0不能解决问题。我假设即使一遍又一遍地安装,在Spyder4或Anaconca环境中的某些东西也不正确。

Downgrading Spyder4 to Python 3.7.4 & IPython 7.8.0 did not solve the problem. I'm assuming that something within Spyder4 or the Anaconca Environment even though I installed it over and over is not correct.

推荐答案

Spyder维护人员),您需要进入菜单

(Spyder maintainer here) You need to go to the menu

运行>每个文件的配置

并激活名为的选项,在控制台的命名空间中运行,而不是一个空的,以便您能够修改代码中的全局变量。

and activate the option called Run in console's namespace instead of an empty one, for you to be able to modify global variables in your code.

这篇关于覆盖函数内部的全局变量不适用于Spyder 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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