English 中文(简体)
有没有一种方法可以使定义或类中的每个变量自动变为全局变量?
原标题:Is there a way to make every variable inside a definition or class to become global automatically?
  • 时间:2010-10-06 16:13:24
  •  标签:
  • python

我在一些定义和类中使用了大量变量(主要是因为我希望能够使用pydev的代码折叠功能)。有没有任何构造函数可以用于定义或类,使其变量自动被视为全局变量?


这是我在遵循评论中提供的一些建议后所做的一个例子:

发件人:

img_globe       = os.path.join(set_img_dir,  img_globe.png )
img_help        = os.path.join(set_img_dir,  img_help.png )
img_exit        = os.path.join(set_img_dir,  img_exit.png )
img_open        = os.path.join(set_img_dir,  img_open.png )
img_tutorial    = os.path.join(set_img_dir,  img_tutorial.png )
img_save        = os.path.join(set_img_dir,  img_save.png )
img_site        = os.path.join(set_img_dir,  img_site.png )

…(长长的列表)

收件人:

varies = {}
dirList=os.listdir(set_img_dir)
for fname in dirList: 
    varies[fname.split(".")[0]] = os.path.join(set_img_dir, fname)
最佳回答

尽管您不应该这样做,而且您正在寻找的解决方案并不像您想象的那么简单,但以下是一个非常简单的示例,说明如何从函数中获取局部变量并使其全局化:

def make_locals_globals():
    """This is just bad"""
    foo = 1
    bar = 2

    locals_dict = locals()
    globals_dict = globals()

    print  Locals: , locals_dict

    for varname, varval in locals_dict.items():
        print  Setting global: %s=%s  % (varname, varval)
        globals_dict[varname] = varval

if __name__ ==  __main__ :
    make_locals_globals()

    print  
Globals: 
    print  foo= , foo
    print  bar= , bar
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签